Ich halte meine Soundpool Proben in allen Aktivitäten geladen

JadLu:

Ich mache eine Gitarre App, die drei Aktivitäten hat und jede Aktivität umfasst eine Audio-Funktion, Grund, warum ich bin mit Soundpool, und ich habe 66 Proben. Mein Problem ist, dass ich sie in jedem laden müssen und jede Aktivität, so meine Frage ist, gibt es eine Möglichkeit, dass ich diese 66 Proben direkt nach meiner app beginnt zu laden, und halte sie in jeder Aktivität geladen?

Jesus liebt dich;

Sie können für eine einfache Utility - Klasse verwenden SoundPool. Sie können eine öffentliche statische Methode verwenden , so dass es einmal instanziiert werden kann und von jeder Aktivität zugegriffen werden. Hier ist eine Klasse , die für Ihren Fall verwendet werden kann:

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.util.Log;

public class SoundPoolManager {
  private static final String TAG = SoundPoolManager.class.getSimpleName();

  private static SoundPool soundPool;
  private static int[] sm;
  private Context context;
  private static float mVolume;

  private static SoundPoolManager instance;
  private static final int SOUND_TOTAL = 1;

  private SoundPoolManager(Context context) {
    this.context = context;
    initSound();

    // add sound here
    // here the sample audio file which can be use with your audio file
    int soundRawId = R.raw.watch_tick;

    //you need to change SOUND_TOTAL for the size of the audio samples.
    sm[sm.length - 1] = soundPool.load(context, soundRawId, 1);
  }

  public static void instantiate(Context context) {
    if(instance == null) instance = new SoundPoolManager(context);
  }

  private void initSound() {
    sm = new int[SOUND_TOTAL];
    int maxStreams = 1;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      soundPool = new SoundPool.Builder()
          .setMaxStreams(maxStreams)
          .build();
    } else {
      soundPool = new SoundPool(maxStreams, AudioManager.STREAM_MUSIC, 0);
    }
    mVolume = setupVolume(context);
  }

  private float setupVolume(Context context) {
    AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    if(am == null) {
      Log.e(TAG, "Can't access AudioManager!");
      return 0;
    }

    float actualVolume = (float) am.getStreamVolume(AudioManager.STREAM_ALARM);
    float maxVolume = (float) am.getStreamMaxVolume(AudioManager.STREAM_ALARM);

    return actualVolume / maxVolume;
  }

  public static void playSound(int index) {
    if(sm == null) {
      Log.e(TAG, "sm is null, this should not happened!");
      return;
    }

    if(soundPool == null) {
      Log.e(TAG, "SoundPool is null, this should not happened!");
      return;
    }

    if(sm.length <= index) {
      Log.e(TAG, "No sound with index = " + index);
      return;
    }

    if(mVolume > 0) {
      soundPool.play(sm[index], mVolume, mVolume, 1, 0, 1f);
    }
  }

  public static void cleanUp() {
    sm = null;
    if(soundPool != null) {
      soundPool.release();
      soundPool = null;
    }
  }
}

Dann können Sie die Klasse mit den folgenden verwenden:

// need to call this for the first time
SoundPoolManager.instantiate(context);

// play the sound based on the index
SoundPoolManager.playSound(index);

// clear up the SoundPool when you don't need it anymore.
SoundPoolManager.cleanUp();

Ich denke du magst

Origin http://43.154.161.224:23101/article/api/json?id=228530&siteId=1
Empfohlen
Rangfolge