3D-kreativ
3D-kreativ

Reputation: 9301

Soundpool not working

I'm doing a simple game and since I need some short sound effects, I thought the Soundpool would be appropriate. But it's not working! I don't get any sounds! I have checked my code over and over again and also compared it with several other examples, but still not working! I'm confused and need some help to get it to work.

Here is my code. Have I missed something or could I do it in another way? preciated some help! Thanks!

soundPool = new SoundPool(4,AudioManager.STREAM_MUSIC, 0);
soundPoolMap = new HashMap<Integer, Integer>();

soundPoolMap.put(0, soundPool.load(this, R.raw.bonk2, 1));
soundPool.play(0, 1f, 1f, 1, 0, 1f);

soundPool.release();

Upvotes: 1

Views: 2761

Answers (1)

Paresh Mayani
Paresh Mayani

Reputation: 128428

In your case, you have mentioned any Sound ID as 0, instead you should mention the Sound ID of the loaded sound using load() method.

For example:

 soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
 soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int sampleId,
                  int status) {
           loaded = true;
       }
 });
 soundID = soundPool.load(this, R.raw.sound1, 1);
 soundPool.play(soundID, volume, volume, 1, 0, 1f);

Check this example: Tutorial: Play sounds via SoundPool

Upvotes: 4

Related Questions