7

I have been trying to get a few sounds to play at the same time; currently i'm using a shared instance of SoundPool. I would like 1, 2 or 3 sounds to be played at the exact same time with no lag.

When calling SoundPool.play(...) X number of times in succession, the sounds are played in that order as one might think. what is the proper what to achieve this where i can prepare all of the sounds to be played at the same time and then play them as one?

Sudo Code:

SoundPool _soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);

_soundPool.load(_context, soundId1, 1);
_soundPool.load(_context, soundId2, 1);
_soundPool.load(_context, soundId3, 1);

_soundPool.play(soundId1, vol, vol, 1, 0, 1f);
_soundPool.play(soundId2, vol, vol, 1, 0, 1f);
_soundPool.play(soundId3, vol, vol, 1, 0, 1f);
Anthony Graglia
  • 5,355
  • 5
  • 46
  • 75
  • 1
    related: http://stackoverflow.com/q/4350212/116249 – Patrick Dec 03 '10 at 23:54
  • @Patrick, I tried the threaded approach and at first i thought it worked, or at least close enough when you just have two threads. but when you take the same idea and scale it up to say 4 threads all doing the same thing then you are back again with the slightest of delays. maybe the next step is to dive into mixing the sounds... i didn't really want to go this far with it but at this point i don't see a lot of other options. – YetAnotherDeveloper Dec 05 '10 at 00:25
  • 1
    I think the sound mixing is the best approach. Did you try it on a phone and not just on an emulator as james stated in the other thread? If you know what sounds you want to play as a mix you can set it up in the beginning of your applications lifecycle, in that way you don't have to mix it when you want to play the sounds, which will hopefully provide a better flow. – Patrick Dec 05 '10 at 08:30
  • 1
    @Patrick, I did try installing it on my Droid X and had the same results as the emulator. I'm sure i could premix the sounds. I hope to have some time later today to read up on sound mixing; i have never done any before but i'm sure it will be a fun exercise. I'll be sure to update this post if i find a good answer. – YetAnotherDeveloper Dec 05 '10 at 16:56

2 Answers2

0

You need to understand that SoundPool.load method is asyncronous, so when you call it 3 times in a row, and then call play, there is no garantee that this sounds actually loaded. So you need to wait until all sounds is loaded. For that use OnLoadCompleteListener:

fun loadAndPlay(soundPool: SoundPool, context: Context, resIds: IntArray) {
    val soundIds = IntArray(resIds.size) {
        soundPool.load(context, resIds[it], 1)
    }

    var numLoaded: Int = 0

    soundPool.setOnLoadCompleteListener { sPool, sampleId, status ->
        numLoaded++

        if (numLoaded == resIds.size) {
            soundPool.setOnLoadCompleteListener(null)

            for (id in soundIds) {
                soundPool.play(id, 1f, 1f, 1, 0, 1f)
            }
        }
    }
}

To use:

loadAndPlay(soundPool, context, intArrayOf(R.raw.sound_1, R.raw.sound_2, R.raw.sound_3))
Oleksandr Albul
  • 1,611
  • 1
  • 23
  • 31
0

i done the sound pool for one sound at time it might help you. Android:sound pool and service

Thank you

Community
  • 1
  • 1
Sameer Z.
  • 3,265
  • 9
  • 48
  • 72