Keep my soundPool Samples loaded in all activities

JadLu :

I am making an Guitar app that have 3 activities and each activity include a audio function, reason why i am using SoundPool, and I have 66 samples. My problem is that I have to load them in each and every activity, so my question is, is there any way that I upload those 66 samples right after my app starts, and keep them loaded in every activity ?

ישו אוהב אותך :

You can use a simple utility class for SoundPool. You can use a public static method so that it can be instantiated once and be accessed from any activity. Here a class that can be used for your case:

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

Then you can use the class with the following:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=123714&siteId=1