SoundPool issue - Unable to play sounds asynchronously ?

All your problems with Audio, Video and Images.

SoundPool issue - Unable to play sounds asynchronously ?

Postby svenmutscher » Tue Aug 18, 2009 1:22 pm

Hi,

it's my first Android project so I have chosen a pretty simple app (for my daughter) to get into:

I created a grid containing several icons showing animals. Touching those, you get to hear
their voices. Every time you hit a new icon, the currrently running sound should be
stopped and the new one should be played instead.

Trying to use a seperate MediaPlayer for each icon causes "interesting" issues and so I decided to
workaround using SoundPool. Well, setting maxStreams to 1, I expected the SoundPool to do exactly
what I want. Unfortunately, the app freezes when I try to start the second sound. My next step was to
set the number of maxStreams a bit higher and stop the last played sound explicitly when starting the
new one. Same problem: Application freezes.

Any idea ?

Here's my code:

public class Sound {

private static Sound instance = new Sound();

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

private int currentStreamId;

private Sound()
{
soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 100);

soundPoolMap = new HashMap<String, Integer>();
}

public static Sound getInstance()
{
return instance;
}

public void addSound(String sound)
{
soundPoolMap.put(sound, soundPool.load(LingoZoo.getContext(), LingoZoo.getContext().getResources().getIdentifier(sound, "raw", "com.mutscher.lingozoo"), 1));
}

public void playSound(String sound)
{
AudioManager mgr = (AudioManager) LingoZoo.getContext().getSystemService(Context.AUDIO_SERVICE);
int streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
// stop currenty playing sound
stopSound(currentStreamId);
// play new sound
currentStreamId = soundPool.play(soundPoolMap.get(sound), streamVolume, streamVolume, 1, 0, 1f);
}

private void stopSound(int streamId)
{
if(streamId != 0)
soundPool.stop(streamId);
}
svenmutscher
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Aug 18, 2009 1:03 pm

Top

Postby mad.dogg » Tue Aug 18, 2009 7:21 pm

Watch the logs when your app freeze. You should see a log about the SoundPool if it is the one who caused it.
Anyway if this is helpful try to change the encoding of the files, give them really small raw audio files, lets say about 100K each.
I am struggling with that API for days now and still I have issues with it. It is quite annoying the SoundPool desides to "give up" you get shit no exception, no callback no nothing, there is alwasy praying of course ... :)
Good luck, and if you beat the damn thing please don't be shy to share your experience.
If it's boring it ain't heavy enough!
mad.dogg
Developer
Developer
 
Posts: 42
Joined: Tue May 20, 2008 2:18 pm
Location: Bulgaria

Postby thopham » Fri Sep 04, 2009 5:27 pm

Hi svenmutscher,

I think you missed a thing on stop() method of SoundPool class. When SoundPool stops a stream (by streamId), it also releases any native resources associated with this stream. So, you have to reload the stream again before doing play it at the next instruction.

Have fun.
thopham
Freshman
Freshman
 
Posts: 3
Joined: Mon May 04, 2009 10:03 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Google [Bot] and 13 guests