私soundPoolサンプルは、すべての活動でロードしてください

JadLu:

私は3つの活性を有し、それぞれの活動は、オーディオ機能、私はSoundPoolを使用しています、と私は66個のサンプルを持っている理由を含めるギターアプリを作っています。私の問題は、私はそれぞれ、すべての活動にそれらをロードする必要があるので、私の質問は、私は右の私のアプリが起動した後、これらの66個のサンプルをアップロードし、あらゆる活動にロードされたそれらを維持することをどのような方法はありますか?

イエスはあなたを愛しています。

あなたはのためのシンプルなユーティリティクラスを使用することができますSoundPoolそれは一度インスタンス化することができ、任意のアクティビティからアクセスできるようにするには、パブリック静的メソッドを使用することができます。ここでは、あなたのケースのために使用することができるクラス。

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;
    }
  }
}

その後、あなたは次のようにクラスを使用することができます。

// 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();

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=228534&siteId=1