0

Since my last question hasnt been answered where I used simple medaiplayer (Sound sometimes remains silent when playing more sounds in a row, why?) and tried to create the whole thing using SoundPool. But guess what, almost same thing happens but with an error

AudioFlinger could not create track, status: -12
Error creating AudioTrack

I read about that at most 32 sound can be stored in the memory of soundpool then I will get the error. Thats right, after the 32th sound I cannot play any of the sounds since I always get the error. So how could I use more than 32 sounds with SoundPool? I tried to use 3 Soundpools with 20 sounds in each of them, and I always unload all the sound from the other pools with this:

    mSoundPoolMapV1.clear();  //clearing HashMap
    for(int i=1;i<15;i++){
    mSoundPoolV1.unload(i);  
    }

But same error comes again. (I use 22k sounds instead of 44.1k, I read this could solve it but nothing happened) Any suggestions appreciated. What the hell is that I cannot play about 50 sounds neither with mediaplayer and nor with soundpool? How is that possible??

With release() :

    mSoundPoolMap.clear();
    for(int i=1;i<50;i++){
    mSoundPool.unload(i);
    }
    mSoundPool.release();

    mSoundPoolMap.put(1, mSoundPool.load(Main.this, R.raw.sound1, 1));
    ...
    mSoundPoolMap.put(50, mSoundPool.load(Main.this, R.raw.sound50, 1));

SO I clear the hashmp, then unload all the sounds, then I release the soundpool. Then I fill the hashmap again, but now It gives me no sound at all. I must miss something, could you tell me what?

Community
  • 1
  • 1
Jani Bela
  • 1,660
  • 4
  • 27
  • 50

2 Answers2

1

There's a limitation in the AudioMixer that only allows 32 AudioTracks at the same time. The SoundPool will try to create an AudioTrack whenever it starts playback of one of the channels (I guess this corresponds to a sample) belonging to the pool. However, the track is not destroyed until you destroy the SoundPool, so the track name will remain allocated even if it has been stopped.

You could try calling release() on your SoundPool. That should cause the native SoundPool object to be destroyed, thereby destroying all AudioTracks it has created and deallocating their names in the AudioMixer.

Michael
  • 57,169
  • 9
  • 80
  • 125
  • I edited my code with releasing the pool, but I do not get any sound played when I touch a button. Do I miss something? – Jani Bela Oct 24 '12 at 15:53
  • Not played ever, or just after you release the first batch and try to load/play another batch? Might help if you post logs taken with your latest code. – Michael Oct 24 '12 at 15:58
  • in the onCreate I load them and they can be played, then I press the button which does the upper code I wrote (releasing as well), then sounds cannot be played. – Jani Bela Oct 24 '12 at 16:01
  • After calling release() you should create a new SoundPool. As the documenation for SoundPool.release() states: "The SoundPool can no longer be used and the reference should be set to null." – Michael Oct 24 '12 at 17:49
  • Thanks shame on me I skipped that part. Thank you for your help it works fine now! I hope it stays like that in the future :) – Jani Bela Oct 24 '12 at 18:15
0

Underlying audio hardware is not unlimited. You can only hold so many sounds in memory at a time. When I made a 3D audio library for desktops the limit was variable so I had to query the limit from the hardware before starting or it would lead to seemingly random failures. You should have a single SoundPool and just load and unload sounds into as you need them. Then you need to set up some kind of a priority for the sounds that should be playing so you can make sure that the sounds you really need don't get unloaded to play an optional background noise.

The SoundPool Documentation actually has a really nice description of a typical use-case.

CaseyB
  • 24,780
  • 14
  • 77
  • 112