5

This question has been asked several times, but so far I have tried every single solution including custom ones and the result is not ideal.

I'm trying to acheieve consistent gapless audio looping in an app and so far I've only achieved it inconsistently.

I have an app that loops audio files which are 14 seconds / 14200ms long. (I exported them all myself to .ogg and the lengths are all the same.

mediaPlayer.setLooping(true); simply does not acheive gapless loops, regardless of file format. I've tried .mp3, .wav, and .ogg for every solution.

The same goes for mediaPlayer1.setOnCompletionListener and alternating between 2 mediaplayers.

My custom solution (which is currently my best), is this:

I calculate how long the file is in milliseconds(14200) and start a runnable which is delayed to start in 14200 - 30 milliseconds. then, play3 runnable would start mediaplayer1 and keep looping.

relevant code:

    durationHandler.postDelayed(play2, mediaPlayer1.getDuration() - 30);
    mediaPlayer1.start();

}

private Runnable play2 = new Runnable() {
    public void run() {
        durationHandler.postDelayed(play3, mediaPlayer2.getDuration() - 30);
            mediaPlayer2.start();

    }
};

This code actually works really well SOMETIMES, but not every time. For example, the first loop might have a little gap but the second loop times out perfectly. My guess is that it has something to do with the actual phone taking a bit longer or shorter of time to process the code. Does that make sense?

I tried soundpool and it won't work for 14 second files.

If anyone has any other suggestions for CONSISTENT gapless looping of 14 second files I'd love to hear them.

user3689720
  • 181
  • 2
  • 11
  • You can use [`setNextMediaPlayer`](http://developer.android.com/reference/android/media/MediaPlayer.html#setNextMediaPlayer(android.media.MediaPlayer)) on devices with API level >= 16, and fall back to one of your current solutions on devices with older Android versions. The older Android versions will become less and less common over time. – Michael Nov 20 '14 at 08:16
  • If you use soundpool, convert sound to ogg and set it to 11KHz mono and it would play the whole 14 seconds, but the gap will still remain for the first loop using postDelay or postAtTime with an handler. Looking for a solution myself. Still, unless you want 44KHz stereo sound, I think soundpool is better in this situation. – abotrix Dec 26 '14 at 08:00

0 Answers0