atom feed13 messages in[android-developers] Re: Does SoundPo...
FromSent OnAttachments
Robert GreenOct 11, 2008 10:11 am 
Robert GreenOct 11, 2008 3:30 pm 
Robert GreenOct 12, 2008 10:32 am 
TjerkWOct 14, 2008 2:07 pm 
Robert GreenOct 14, 2008 3:43 pm 
blindfoldOct 15, 2008 1:01 am 
Robert GreenOct 15, 2008 6:47 am 
plusminusNov 10, 2008 8:04 pm 
Robert GreenNov 11, 2008 6:43 am 
plusminusNov 11, 2008 10:10 am 
DaveNov 11, 2008 11:24 am 
plusminusNov 12, 2008 7:57 am 
DaveNov 12, 2008 8:27 am 
Subject:[android-developers] Re: Does SoundPool work or has anyone successfully used it?
From:TjerkW (
Date:Oct 14, 2008 2:07:35 pm

I also wanted to use the soundpool, but lack of documentation made it impossible to use. I tried it they way i would think it would work, but it didn't.

I am using mediaplayers now, i cannot play the same sound multiple times concurrently, Too bad.

On 12 okt, 19:32, Robert Green <> wrote:

I have a few more comments on this class.

1)  Is it going to be officially supported?

2)  Here are some problems I've found with it and workarounds I used:

Bug #1) Looping an mp3 crashes around the 8th loop but looping waves seems to work fine. Bug #2) SoundPool.stop(streamId) immediately crashes the app. Bug #3) setRate() with a rate over 1.5 either doesn't work or also crashes the VM with no exception.

My new workaround for lack of stop():

to stop a stream, call setLoop(streamId, 0) then setVolume(streamId, 0f, 0f). This will stop a loop after it's done but drop the volume so it seems to the user to have quit right then. After that, you don't need to call stop() because the stream will clean itself up from the loop stopping. This worked for me.

Even if they haven't readied it for public use, it's the only suitable API forsoundin games. I looked into using MediaPlayers but if you have more than a couple of sounds and you need to be able to play the samesounda few times concurrently, it's just not a feasible solution.

I'm really surprised that there is no access to the audio buffer. How are we supposed to write dynamic audio generation apps?

Androidreally needs 2 things to be competitive with the audio side:

1) Public access to the media decoders (so a dev can decode an mp3 into raw) 2) Public access to asoundstream output buffer (so a dev can pipe in rawsounddata)

On Oct 11, 5:30 pm, Robert Green <> wrote:

Problem solved.  Here's how I did it:

This only loads onesoundbut you can load as many as you want. I have an mp3 in my res/raw directory called explosion.mp3.


          public static final int SOUND_EXPLOSION = 1;           public static final int SOUND_YOU_WIN = 2;           public static final int SOUND_YOU_LOSE = 3;

          private SoundPool soundPool;           private HashMap<Integer, Integer> soundPoolMap;

          private void initSounds() {                soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);                soundPoolMap = new HashMap<Integer, Integer>();                soundPoolMap.put(SOUND_EXPLOSION, soundPool.load(getContext(), R.raw.explosion, 1));           }

          public void playSound(intsound) {                AudioManager mgr = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);                int streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);      , streamVolume, streamVolume, 1, 0, 1f);           }

          public void update() {                if (isExploding()) {                      playSound(SOUND_EXPLOSION);                }           }

I'm addingsoundinto my game now and need the ability to havesound effects concurrent (like 2 of the same explosion playing at the same time, etc) so using the MediaPlayer is less than optimal for me. I was thinking of just adding a traditionalsoundupdate into the main loop which would probably work and isn't _that_ hard to write but I don't think I have access to the decoder andsoundbuffer.

I found a class called SoundPool that appears to be suited for exactly what I'm trying to do however the documentation is very weak

Has anyone used this with success? If so, could I see a little code snippet?

I tried this: Java:

          private void initSounds() {                // 4 streams                // stream type 3 is music                soundPool = new SoundPool(4, 3, 100);                int explosionSound = soundPool.load(getContext(), R.raw.explosion, 1);      , 100, 100, 1, 0, 1);           }

but it didn't appear to do anything. I didn't get errors but I also heard nosound. Perhaps I need to configure something for the emulator to work like that?