Android Sound Programming

Thought i’d post this for those out there doing some sound programming on Android phones. Ran into a really annoying bug/limitation while trying to get sounds working. To get sounds and music playing in your apps you have two options, a rather simple MediaPlayer class and slightly more complicated SoundPool class. Being a previous sound programmer I of course opt for more control and go with creating my own sound manager using SoundPool. It all worked great playing little sounds and such until I inserted a song to play. The song wouldn’t play at all or would only play for 5 seconds or so then suddenly cut out. It seems there’s a 1MB limit when working with sounds in the SoundPool, go over that limit and weird things happen. Of course this is completely undocumented (Android docs are good for very basic knowledge of getting set up, but anything beyond that and you’re on your own). 1MB is pretty limiting when it comes to music, I had a small theme song I was trying to play in the background of my app, it was around 800kb as an MP3, but uncompressed it comes out at around 6MB. After several days of pulling my hair out I finally ran across someone that had actually dug into the android code and found this 1MB limit in SoundPool. MediaPlayer on the other hand happily takes large sound files and plays them just fine. So, lesson learned, use SoundPool for more control and small sound effects in your app, and if you need to play background music or larger sound files you may have to go with MediaPlayer. Hopefully this will save somebody out there some headache as they work with sound on Android.

Some errors you might see in LogCat when running into this problem:
AudioFlinger could not create track, status: -12
Error creating audio track
Something about buffer underuns


About

Leave a Reply

Your email address will not be published. Required fields are marked *