Android: MediaPlayer stops playing sounds and AudioFlinger starts throwing "no more track names availlable"

This error occurs when you've created too many MediaPlayer instances without cleaning up your own filthy mess.

AudioFlinger: no more track names availlable
AudioTrack: AudioFlinger could not create track, status: -12
AudioSink: Unable to create audio track
VorbisPlayer: mAudioSink open failed

Instead, you should either reuse MediaPlayer instances or use the SoundPool class.

Note: The strange way of spelling "availlable" is a typo in the framework.

Reuse MediaPlayer

If you want to reuse the MediaPlayer, just remember to clean up after yourself and call MediaPlayer.release() after you're done.

Otherwise, dedicate one MediaPlayer instance to each sound effect and reuse it each time you play that specific sound.

Use SoundPool

This class manages multiple sounds for you, so you can set the maximum number of channels. It also handles stuff like left/right speaker volume, pausing, stopping, etc.

The annoying thing however is that it returns its own "Sound ID", which you have to keep track of. You get the new sound ID after you've loaded it up.

I've written a small class which uses a HashMap to keep track of them.

public class Sound {
private SoundPool m_soundPool;
private HashMap<Integer, Integer> m_soundIDs;

public Sound() {
int[] sounds = { R.raw.menu, R.raw.dizzy, R.raw.pokewrong, R.raw.go, R.raw.ready, R.raw.win, R.raw.music, };

m_soundIDs = new HashMap<Integer, Integer>();
m_soundPool = new SoundPool(8, AudioManager.STREAM_MUSIC, 0);

for (int sfx : sounds) {
m_soundIDs.put(sfx, m_soundPool.load(G.activity, sfx, 1));
}

}

public void play(int resid) {
if (!G.settings.sfx) {
return;
}

m_soundPool.play(m_soundIDs.get(resid), 1.0f, 1.0f, 1, 0, 1.0f);
}

Using this class, all you really have to pass in to play the sound is the resource ID.

Sources

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog