16

I'm having troubles with the soundpool class. Here it goes:

In my game app (music app btw) I need to reproduce at least 32 short sounds at the same time, so I declare my soundpool like that:

private SoundPool sp;
sp = new SoundPool(128, AudioManager.STREAM_MUSIC, 0);

After that I load all the MP3 sounds needed, about 80 sound of 55KB each. I have no troubles loading all the sounds, but its slow! Well it's no the problem. The real trouble is when I play about 20 sounds at the same time, there's an error in my log:

ERROR/AudioFlinger(59): no more track names available
ERROR/AudioTrack(26349): AudioFlinger could not create track, status: -12
ERROR/SoundPool(26349): Error creating AudioTrack

After that every sound that i try to play throws the same error, and no sound can be played. Not even sounds of another Activity / soundpool. I have no clue of what's going or how to fix it! Should I change the format of the sound files? Should I free memory or something after playing a sound?

(I'm testing on a Samsung Galaxy S I9000, 2.3.3 OS system. The app is 2.1)

skynet
  • 9,898
  • 5
  • 43
  • 52
Carles
  • 451
  • 5
  • 16
  • Perhaps you need to implement SoundPool.OnLoadCompleteListener ? – Codeman Aug 03 '12 at 18:40
  • to call release() for each of the AudioTracks ? – Codeman Aug 03 '12 at 18:46
  • Make sure that loop mode is not enabled, no track is set to pause and every mp3 is really that short as you think (no silence at the end). As there is a limited amount of audio tracks available, you have to keep the amount of running audio tracks low. Maybe there are less than 32 tracks available, so you cannot play that many sounds at the same time. Also some implementations are buggy, and may hang on some tracks (maybe dependend on a bad mp3 file or something) thus the available tracks are used up quite fast. If the application crashes, sometimes the tracks aren't freed, so no sound. – dronus Aug 11 '12 at 13:12

2 Answers2

7

see this (in android group)

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),

ifreedom
  • 426
  • 3
  • 4
1

A couple of thoughts here. One: the first parameter to the SoundPool constructor is not the number of sounds you want to load into it, it's the maximum number of simultaneous streams that you'll be playing. Second, SoundPool has limited memory for sounds, about 1MB. So I wouldn't be at all surprised if you hit some undocumented limit to the number of tracks you can load in at one time. Notice that 80 sounds times 55k per sound is definitely over 1MB. And that limit is for after the mp3s have been uncompressed into audio data inside SoundPool.

Dave MacLean
  • 5,163
  • 2
  • 22
  • 31