Question

I've currently got a SoundPool that loads and plays a single sound when the screen is shaken, how would I go about it playing a different sound each time the screen is shaken?

EDIT ---

private SensorManager mSensorManager;
private ShakeEventListener mSensorListener;
SoundPool sp;
int soundOne;
int soundTwo;
int soundThree;

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);
    //Shake sound
    sp =  new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
    soundOne = sp.load(this, R.raw.one, 1);
    soundTwo = sp.load(this, R.raw.two, 1);
    soundThree = sp.load(this, R.raw.three, 1);     

    //Shake
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mSensorListener = new ShakeEventListener();   
    mSensorListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {

        public void onShake() {
            if (acceptCheck == 1)
            {
                sp.play(soundOne, 1, 1, 0, 0, 1);
            }
        }
      });




}
Was it helpful?

Solution

Just use a list of ints instead of storing them all individually:

private SensorManager mSensorManager;
private ShakeEventListener mSensorListener;
SoundPool sp;
List<Integer> mSounds = new ArrayList<Integer>();
private Random mRandom = new Random();

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);
    //Shake sound
    sp =  new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
    mSounds.add(sp.load(this, R.raw.one, 1));
    mSounds.add(sp.load(this, R.raw.two, 1));
    mSounds.add(sp.load(this, R.raw.three, 1);     

    //Shake
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mSensorListener = new ShakeEventListener();   
    mSensorListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {

        public void onShake() {
            if (acceptCheck == 1)
            {
                sp.play(mSounds.get(mRandom.nextInt(mSounds.size())), 1, 1, 0, 0, 1);
            }
        }
      });
}

OTHER TIPS

First you can create an array with all your raw's references:

private int[] rawRef = {R.raw.1,R.raw.2,R.raw.3,R.raw.4,R.raw.5};

Then you need a method to play your audio:

private Random random = new Random();

private void play() {
   MediaPlayer mp;
   try {
      mp = MediaPlayer.create(this, rawRef[random.nextInt(rawRef.length)]);
      mp.start();
   } catch (Exception e) {
      Log.e("ERROR", "Media Player", e);
      mp = null;
   }
}

For SoundPool

    private List pool = new ArrayList();
    private SoundPool sp;

    private void loadSounds(){
       sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
       pool.add(sp.load(context, R.raw.1, 1));
       pool.add(sp.load(context, R.raw.2, 1));
       pool.add(sp.load(context, R.raw.3, 1));
       pool.add(sp.load(context, R.raw.4, 1));
    }

    private void play(){
       sp.play(pool.get(random.nextInt(pool.size()), 1, 1, 0, 0, 1);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top