Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AudioFlinger could not create track. status: -12

I am programming for android 2.2 and am trying to using the SoundPool class to play several sounds simultaneously but at what feel like random times sound will stop coming out of the speakers.

for each sound that would have been played this is printed in the logcat:

AudioFlinger could not create track. status: -12  Error creating AudioTrack Audio track delete 

No exception is thrown and the program continues to execute without any changes except for the lack of volume. I've had a really hard time tracking down what conditions cause the error or recreating it after it happens. I can't find the error in the documentation anywhere and am pretty much at a loss.

Any help would be greatly appreciated!

Edit: I forgot to mention that I am loading mp3 files, not ogg.

like image 712
user1599837 Avatar asked Aug 15 '12 05:08

user1599837


2 Answers

i had almost this exact same problem with some sounds i was attempting to load and play recently.

i even broke it down to loading a single mp3 that was causing this error.

one thing i noted: when i loaded with a loop of -1, it would fail with the "status 12" error, but when i loaded it to loop 0 times, it would succeed. even attempting to load 1 time failed.

the final solution was to open the mp3 in an audio editor and re-edit it with slightly lesser quality so that the file is now smaller, and doesn't seem to take up quite as many resources in the system.

finally, there is this discussion that encourages performing a release on the objects you are using, because there is indeed a hard limit on the resources that can be used, and it is system-wide, so if you use several of the resources, other apps will not be able to use them.

https://groups.google.com/forum/#!topic/android-platform/tyITQ09vV3s/discussion%5B1-25%5D

For audio, there's a hard limit of 32 active AudioTrack objects per device (not per app: you need to share those 32 with rest of the system), and AudioTrack is used internally beneath SoundPool, ToneGenerator, MediaPlayer, native audio based on OpenSL ES, etc. But the actual AudioTrack limit is < 32; it depends more on soft factors such as memory, CPU load, etc. Also note that the limiter in the Android audio mixer does not currently have dynamic range compression, so it is possible to clip if you have a large number of active sounds and they're all loud.

For video players the limit is much much lower due to the intense load that video puts on the device.

I'll use this as an opportunity to remind media developers: please remember to call release() for media objects when your app is paused. This frees up the underlying resources that other apps will need. Don't rely on the media objects being cleaned up in finalize by the garbage collector, as that has unpredictable timing.

like image 175
john.k.doe Avatar answered Sep 20 '22 23:09

john.k.doe


I had a similar issue where the music tracker within my Android game would drop notes and I got the Audioflinger error (although my status was -22). I got it working however so this might help some people.

The problem occurred when a single sample was being output multiple times simultaneously. So in my case it was a single sample being played on two or more tracks. This seemed to occasionally deadlock or something and one of the two notes would be dropped. The solution was to have two copies of the sample (two actual ogg files - identical but both in the assets). Then on each track even although I was playing the same sample, it was coming from a different file. This totally fixed the issue for me.

Not sure why it works as I cache the samples into memory, but even loading the same file into two different sounds didn't fix it. Only when the samples came out of two different files did the errors go away.

I'm sure this won't help everyone and it's not the prettiest fix but it might help someone.

like image 44
user2016617 Avatar answered Sep 21 '22 23:09

user2016617