Reputation: 2607
I have a button that when pressed sounds a short sound effect with SoundPool. the file is a 16k mp3.
The sound effect works for hundreds of button clicks, but at some point i get this error (and the sound isn't playing for clicks anymore):
E/AudioFlinger( 34): no more track names available
E/AudioTrack( 904): AudioFlinger could not create track, status: -12
E/SoundPool( 904): Error creating AudioTrack
W/AudioFlinger( 34): write blocked for 81 msecs, 4573 delayed writes, thread
xb3f0
onCreate:
setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundpool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundpool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
// TODO Auto-generated method stub
loaded = true;
}
});
//save the effect somewhere:
MyUtil.regularSound = soundpool.load(this, R.raw.regular_click,1);
onClick:
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVol = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVol = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float vol = actualVol/maxVol;
if (loaded) {
soundpool.play(MyUtil.regularSound,vol,vol,1,0,1f);
}
before starting another intent:
Intent i = new Intent(con, EndScreen.class);
if (soundpool != null) {
soundpool.release();
soundpool = null;
}
startActivity(i);
Upvotes: 2
Views: 1825
Reputation: 41
I just spent hours with the same problem.
The -12 error means that sound allocated to the app is out of memory. using .finish()
doesn't release the memory of SoundPool. You have to use .release()
.
In my app I have a soundPool.release() on the onclick method that starts the next activity,
Changing the sound formats didn't work for me either.
Upvotes: 4
Reputation: 31
You try code it: here
/*
private SoundPool mSoundPool;
private SparseArray<Integer> mSoundPoolMap;
private AudioManager mAudioManager;
*/
public static void clear() {
if (mSoundManager != null) {
mSoundManager.mSoundPool = null;
mSoundManager.mAudioManager = null;
mSoundManager.mSoundPoolMap = null;
}
mSoundManager = null;
}
Upvotes: 0