There goes SoundPool
Oct 8th update: The 2.3.5 update for SGS2 seems to fix the problem. At least I can’t reproduce the problem anymore. I don’t know how quickly the update will roll out for all the different versions of the phone and in different regions, but if the problem indeed has been fixed, it might not be worth the trouble to work around it. Unless of course you get other benefits from OpenSL.
I received feedback in June about DiceShaker D&D crashing on the then new Samsung Galaxy S2. After finally getting a proper excuse to buy one, I was quickly able to reproduce the problem. Looking at the stack trace produced by the crash, it seemed to be caused by SoundPool.
I/DEBUG (20675): *** *** *** *** *** *** *** *** *** *** *** I/DEBUG (20675): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr deadbaad I/DEBUG (20675): #12 pc 000046ae /system/lib/libsoundpool.so
SoundPool is one of the available options for playing sounds in an Android application, perhaps the best / easiest to use for game sound effects. Googling “galaxy s2 soundpool” I found out that other app developers had also had the same problem reported, and made the same conclusion about the cause. The problem had been reported to the Andoid bug tracker on June 13th. Reducing the number of audio channels used seemed to gradually make the SoundPool crashes less frequent, but I couldn’t get completely rid of the problem until reducing the channels to one. That didn’t sound like a good solution, so I temporarily excluded the SGS2 from the supported devices list, and hoped that Samsung/Google would fix the problem shortly – it was constantly crashing even high profile titles like Cut the Rope (on Gift Box levels), so it might get some priority.
Three months after the bug had been reported, Samsung released Android 2.3.4 for the SGS2. The problem was still there. Some comments on the bug tracker claim the problem exists also on the leaked 2.3.5 update. Researching better workarounds for the problem I came across Gray Fin Studios’ account on how they fixed the problem using OpenSL ES. OpenSL ES is a native audio library introduced in Android 2.3, and while it wouldn’t work on older devices, one could make their app automatically use it if it was available and fall back to SoundPool on older OS’es, which didn’t have the problem. After reading about Gray Fin’s positive experiences with OpenSL ES, I quickly hacked together a simple SoundPool’esque audio implementation on top of it.
Some quick initial testing seems to agree with Gray Fin’s findings. SoundPool usually crashes after playing some random samples in a loop for less than a minute, the OpenSL seems not to. The OpenSL implementation is limited at the moment, but may perhaps be of some use if you’ve just started looking into workarounds. It’s source code is available at http://code.google.com/p/opensl-soundpool.
I haven’t considered fragmentation a huge problem for Android so far, but I think this is a good example of the kind of problems the huge amount of different makes and models can bring. Until last spring SoundPool was a proper choice for producing sound effects in an Android app, then someone releases a device on which it doesn’t work and sells 10 million units of the said device in 5 months, and suddenly you just can’t use that part of the Android API anymore.
The following handset share statistics from a Finland-only-app CallerInfo kind of underlines the point about having to support Galaxy S2 – it’s already huge (SGS2 has been available here since April).
Tags:



30. November 2011 at 09:30
First of all, thank you for writing up this great article and sharing your code. My app is getting one-star ratings because of the SGS2 crashes, so having a way to deal with it is great!
I was able to check out your code an successfully run it, including getting your native lib to work.
But for some reason, when I move it to my own project, the lib is not loading. I’m getting “java.lang.UnsatisfiedLinkError: Couldn’t load opensl-soundpool: findLibrary returned null”.
I haven’t dealt a lot with native libs, so I’m not sure how to troubleshoot it. Do you have any ideas why the lib might not be loading in a different project?
Thanks in advance!
30. November 2011 at 11:05
I found my problem, so I though I would post for others who might run into it:
I had other libraries, that had both armeabi and areabi-v7a versions. I only put the libopensl-soundpool.so file in the armeabi directory. But apparently when the apk gets installed, android only grabs all the files from one directory or another. It doesn’t cherry pick files individually if they are missing from one directory or another.
So the answer was to make sure the .so file was in both lib directories. Problem solved!
30. November 2011 at 11:06
Thanks, hope it works for you.
Sounds like for some reason the native library is not included in the build. Have you tried unzipping the built APK file to see if the libopensl-soundpool.so is included in it? How are you including it in your own project? If your project is not using NDK, just creating a libs/armeabi folder under your project root and copying the .so file into it should be enough.
30. November 2011 at 11:09
Ok, glad you got it solved. Let me know if you have issues and whether it fixes the problem.
1. December 2011 at 10:30
It did fix the problem. Thanks.
But I do have another question. I appears that I can only get .wav files to work with it. I noticed you only used .wav files in your example. Was that a coincidence? Is there something else I have to do to get them to get other formats to work?
1. December 2011 at 11:36
Android’s OpenSL doesn’t support native mp3/ogg decoding prior to API level 14 (Android 4.0 / ICS), and my code doesn’t include any 3rd party decoders, so currently it unfortunately only supports wav files.
The solution would be to include some decoder library (eg. for ogg xiph.org’s libvorbis or tremor) to the build and modifying OpenSLSoundPool->load function to use the decoder. I might add this at some point, but if you’re in a hurry, your options are either to only use it with wavs, or add the decoding part yourself.
12. January 2012 at 11:38
in my project,libopensl-soundpool.so in libs,but “java.lang.UnsatisfiedLinkError: createEngine”