79

I was looking at Android's SoundPool as a mechanism to implement sound effects in my generic game development library. It seemed ideal.

But a little bit of research indicates that there all kinds of bugs in SoundPool. Are the bugs in SoundPool still relevant?

Because I'm developing a library, any bugs in SoundPool become bugs in my library, and I want to insulate my users from that.

So my question is basically: what API should I use for audio?

Using AudioTrack and writing my own mixer is not out of the question. But obviously it would be preferable to avoid doing that. And is there any API to provide decoding for me?

I need to be able to play a reasonable number of simultaneous sound effects (at least 16, let's say), and have even more open. Sounds need to start playing with low latency. WAV files need to be supported (MP3/Ogg is unimportant). Sound effects need to support seamless looping and dynamic, individual volume adjustment. The Android app lifecycle needs to be properly supported.

I have heard there is a 1MB limit somewhere for SoundPool, this is probably acceptable for each individual sound effect but not for all buffers/sounds. Can someone tell me exactly what the limit is on?

Finally, I need to be able to play background music as well, in compressed formats, with low CPU load. I assume MediaPlayer is ideal for this. Can it be used in parallel with another API?

I know a few people have been using MediaPlayer to fill in for SoundPool. But does it support the features that I need?

Are there any other audio APIs I've missed?

Community
  • 1
  • 1
Andrew Russell
  • 26,924
  • 7
  • 58
  • 104
  • 3
    I looked at the bugs you linked to, and none of them seem very catastrophic. Maybe I'm missing something but I would say just use it and as you find bugs report them. Kudos on you for doing your homework! – slf Jun 28 '11 at 00:47
  • @slf I should point out that those are just random examples - there are reports of crash bugs (both app and device) and no-audio bugs floating around the Internet. There are *lots* of them - but I cannot tell if its because `SoundPool` is buggy or people just aren't using it correctly or something. There are a few places that suggest some of the worst bugs are device-specific. And is it just one or two old devices I can ignore? Or are their many devices requiring many different workarounds? (Which I don't have the resources to support.) – Andrew Russell Jun 28 '11 at 03:49
  • @AndrewRussell What came of this? SoundPool is not handling ogg files of 90K giving the classic 1M log output - totally stupid. I have the same requirements as you. Any tips would be appreciated. – zaf Feb 15 '12 at 19:12
  • My app has problem playing sounds on Samsung Galaxy S2, I googled around and found SoundPool class to be the culprit, check this out for more http://www.cocos2d-x.org/boards/10/topics/7980, apparently the soundpool class has problems playing sounds in dual core phones. So it is better to avoid it... – Arif Nadeem Mar 28 '12 at 15:03
  • @AndrewRussell May I ask what did you end up doing? I playing around with the library for some time and so far it is not working very well – Efi G Aug 14 '19 at 10:56
  • @EfiG Stopped developing for Android ;) – Andrew Russell Aug 18 '19 at 04:56
  • @AndrewRussell Lucky guy:) – Efi G Aug 18 '19 at 07:42

3 Answers3

17

Just to add some more recent feedback on this issue. I've been using SoundPool for some time in an app with a fairly large user base for key press sounds. Our use case:

  • Must be played immediately
  • Up to 3+ sounds in parallel
  • We make use of the setRate across it's full range [0.5f-2.0f]

I've now experienced two major device specific issue and have decided to cut my losses and switch away from SoundPool

  • A large number of 4.4 LG devices (mostly the LG G2/G3 line) were having a native crash with their implementation of SoundPool. This was fixed in an update (eventually) but we still have a lot of users with un-upgraded devices
  • Sony Xperia devices currently have all sorts of issue with SoundPool as reported by others. In my case, I've discovered that if you use setRate with rate > 1.0f the SoundPool with start throwing exceptions until your app quits (and burn through a bunch of battery in the process).

TL;DR; I no longer think it's worth the danger/hassle of debugging SoundPool

Sparky
  • 327
  • 2
  • 9
  • It's absurd that this is still an issue. If I ever slog back into Android dev (*shudder*), then I sure as hell won't be using SoundPool. Accepted. – Andrew Russell Jan 28 '15 at 02:53
  • As a further, follow up. It seems that on some of these devices the issues run deeper than just `SoundPool`. Adjusting playback rate on the Xperia still has issues when I'm using an `AudioTrack`. But on the upside I seem to be able to work around them via `AudioTrack`, not the case with `SoundPool` – Sparky Feb 12 '15 at 23:33
  • 3
    @Sparky So what are you using currently instead of SoundPool? `AudioTrack` only with your own mixer implementation? Or any other library? – bogumil Jul 20 '16 at 11:19
  • 1
    @Sparky and others that have input: what did you go with as a soundpool alternative? – IcedDante Feb 21 '17 at 23:00
  • 2
    @Sparky inquisitive minds want to know what you've used instead... tell us your secret :-p – Someone Somewhere Sep 26 '17 at 16:25
9

Stick with OGG files and SoundPool will do you just fine. It's the nature of the multi-platform beast that is Android that there WILL be hardware configurations that will not work with every significant program, no matter how diligently the programmers try.

If this is a large and well-funded project, add to the funding one of each major phone for testing. It's actually much cheaper than the programmer time spent researching and trying to guess what their performance is.

Sorry. Seems as if this isn't the answer that you were looking for. Good luck!

SMBiggs
  • 11,034
  • 6
  • 68
  • 83
  • it has not been my experience that using OGG files instead of MP3 files will work just fine. i had mp3 files that were 3 KB, 3 KB, 8 KB, 16KB, 52 KB & 52 KB, each tied to a different gesture . i could never load them all; whichever was the last of the largest 3 loaded would result in 'AudioFlinger could not create track, status: -12'. i tried changing them all to .ogg files, and i had the exact same results. – john.k.doe Aug 19 '12 at 20:36
  • Hmmm, your file sizes should be no problem. Any solutions? – SMBiggs Aug 22 '12 at 13:29
  • i did not need super high performance, so i've gone with a hybrid approach; the smaller files (and i've added more of them; i now have 11) are in a soundpool (and all of them, ironically, are still mp3), and the larger files each get their own MediaPlayer. still seems buggy; occasionally, one of the soundpool files seems not to load, and i'm trying to diagnose why. but none of them are over 11KB . – john.k.doe Aug 22 '12 at 15:31
  • Are you using Services or some other threads? Perhaps you're creating more than one copy of SoundPool or some manager? My gut reaction to your description is that something is stepping on another thing's toes. The files are truly trivial in size--that should not be an issue at all. I suggest making your issue it's own question. You'll get more eyes on it that way and a chance to explain (and supply code) in greater detail. – SMBiggs Aug 24 '12 at 05:20
  • scott, i might do that, because the issue is more complicated than this has room for. my only point in my original comment was that changing even small mp3 files to ogg files in and of itself did not solve the problem, whereas taking these small resources to their own MediaPlayer objects took care of the problem. (i will say that i think it's possible that setting continuous looping on some of the sounds may contribute to the problem; wish i had more time to debug it to know for certain … and thus asking the question separately may indeed be a good idea when i have time …) – john.k.doe Aug 24 '12 at 07:50
3

DISCLAIMER: I have a small amount of experience with MediaPlayer, and no successful experience with the other APIs I mention, and the following information is based on what I've read in the DOCs and what I've read from google searches.

You could use mediaplayer (for the background music) with other audio APIs, since MediaPlayer automatically runs on it's own thread, but I believe it has a high-ish cpu load, and I don't think it would take compressed bits very well, but I'm not too sure.

There's also JetPlayer http://developer.android.com/reference/android/media/JetPlayer.html which seems like a lot of work to use effectively, but it would work very well with playing background music, then playing other sounds as needed in the game. From what I read of the DOCs, it takes a MIDI file (I think?) and you mute and unmute tracks to make it work how you want it to.

I like AudioTrack because it gives you the ability to edit sounds at runtime by changing the frequencies of the sound, and SoundPool can do the same.

Though for your situation, AudioTrack doesn't seem like it would work well, since playing two sounds would require two threads because AudioTrack is blocking (I'm pretty sure).

And with SoundPool, I'm thinking that since you have 16 sounds, maybe take two threads with one SoundPool in each thread and apply 8 sounds to each SoundPool. I don't really know though, as I've never even tried using SoundPool.

And again, my information is not based on experience, just what it appears from what I've read, so I may be completely or maybe just slightly wrong, or heck, who knows.

And I don't really know anything about the SoundPool bugs, since I haven't researched it.

Reed
  • 14,703
  • 8
  • 66
  • 110