I would like to use two soundpools in my activity. One that I will unload and release sometimes and load it with different sounds, then I unload/release again and use it again with other sounds, and another one that will always contain the same sounds and will never be unloaded. I didnt find any words about this in the documentation, so will it cause me any kind of problem?
And if I use the constant sounds with MediaPlayer instead of SoundPool? Will it cause any memory leak or problem?
I would like to use two soundpools in my activity. One that I will
Share
There will be no memory leaks or sound playback issues, if you correctly handle your
SoundPoolandMediaPlayerobjects in terms of yourActivitylifecycle (see below).First of all, you shouldn’t make “too many”
SoundPoolandMediaPlayerobjects. This depends on the actual state of your device, but I’m sure you’ll be OK with 2SoundPoolobjects and e.g. 4MediaPlayerobjects, simultaneously.Explanation: each
SoundPoolreserves one nativeAudioTrack, and eachMediaPlayeralso reserves one nativeAudioTrack. Therefore, with 2SoundPooland 4MediaPlayerobjects, you will have 6 underlyingAudioTrackobjects in use. This is usually fine, as the system usually has many more freeAudioTrackobjects. (For more detailed information, see this topic.)Regarding your concrete question: yes, it’s also OK to use a permanent
SoundPooland anotherSoundPoolwhich you recreate/release sometimes, as long as you take into account the following point:onPause()oronStop()event handler of your activity, you should release allSoundPoolandMediaPlayerobjects, because they use native resources. Then in youronResume()oronStart(), you can recreate them again. So your statement “… will never be unloaded” isn’t correct in this sense, because you need to handle the state ofSoundPoolandMediaPlayerobjects across yourActivitylifecycle.