Skip to content Skip to sidebar Skip to footer

Lags While Playing 2 Sounds On The Same Time In Timer

I have a small app that basically sets up a timer and plays sets of 2 sounds one after another. I've tried 2 timers, because I wanted both sounds to start exactly at the same time

Solution 1:

I know it may be a little too much, but you could try a different implementation of SoundPool:

publicclassSound {
    SoundPool soundPool;
    int soundID;

    publicSound(SoundPool soundPool, int soundID) {
        this.soundPool = soundPool;
        this.soundID = soundID;
    }

    publicvoidplay(float volume) {
        soundPool.play(soundID, volume, volume, 0, 0, 1);
    }

    publicvoiddispose() {
        soundPool.unload(soundID);
    }
}

To use it you can do something like this:

SoundPoolsoundPool=newSoundPool(20, AudioManager.STREAM_MUSIC, 0);
AssetFileDescriptorassetFileDescriptor= activity.getAssets().openFd(fileName);
intsoundID= soundPool.load(assetFileDescriptor, 0);

Soundsound=newSound(soundPool, soundID);

And then play:

sound.play(volume);

P.S. If the problem persists even with this code, post a comment and I'll get back to you.

Solution 2:

Are you using SoundPool? It is quicker than MediaPlayer. The best chance of getting the two sounds together is when playing them on the same thread.

Post a Comment for "Lags While Playing 2 Sounds On The Same Time In Timer"