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


