6

When trying to loop MP3 files there is a very very slight gap on my KitKat and Lollipop devices, but on an old Gingerbread phone, the gap between loops is more pronounced. It's enough that I need to do something about it so.....

After much research, I learned that MP3 files put a little bit of 'padding' at the beginning of the file, therefore, when looping, it doesn't give a 'clean loop' and this effect can be more pronounced on older/slower phones (apparently).

So, the advise I read was to switch to using OGG Vorbis files. These don't have any blank space/padding and 'loop perfectly'.

However, after attempting multiple times, I'm actually getting worse results with OGG files than I am with MP3's - but the problem is the opposite, ie when the loop is supposed to go back to the start of the OGG file, it actually seems to start again but from about 2 or 3 seconds into it (ie no 'gap', it just doesn't loop correctly).

I should point out the the OGG files themselves seem OK, they loop perfectly in Audacity.

I'm working with 2 MediaPlayer objects - one for an intro and one for the main loop, the intro is about 30 seconds in length and the main loop is about 3 minutes in length.

I'm also (for JellyBean and higher devices), using setNextMediaPlayer to transition from the intro to the main loop - this works OK (or at least it does on the two devices on which I've tested it) - and for pre JellyBean devices I'm starting my main loop in the onCompletitionListener callback method - I only have one pre-JB device, but again, the transition seems to be OK).

Note sure if it helps, but here is some code, although the issues does appear to be with how the Android MediaPlayer handles OGG Vorbis files.

I've been trying to get this looping working correctly for almost a week, so hoping someone has come across this before and can throw some light on it.

public void setDataSource() {

    try {

        loopPlayer.reset();
        loopPlayer.setDataSource(myContext, Uri.parse("android.resource://com.me.myapp/" + R.raw.loop));
        loopPlayer.prepare();
        loopPlayer.setLooping(true);

        introPlayer.reset();
        introPlayer.setDataSource(myContext, Uri.parse("android.resource://com.me.myapp/" + R.raw.intro));
        introPlayer.prepare();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                introPlayer.setNextMediaPlayer(loopPlayer);
                loopPlayer.setLooping(true);
            }

            introPlayer.setOnCompletionListener(this);      

    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}

@Override
public void onCompletion(MediaPlayer mp){

    //If OS < Jelly Bean, then start loop
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN){
        loopPlayer.start();
    }
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Zippy
  • 3,826
  • 5
  • 43
  • 96
  • Have your tried any alternative method to play your file ? I would try this: https://github.com/wseemann/FFmpegMediaPlayer – csenga Nov 24 '15 at 18:20
  • According to the official docs, gapless playback was only added in Jellybean: https://developer.android.com/about/versions/android-4.1.html#Gapless, so I guess you are out of luck. – Jeroen Mols Nov 25 '15 at 14:54
  • One (complicated) thing you could try is [decode a Wav file to a byte[] array](https://stackoverflow.com/questions/10397272/wav-file-convert-to-byte-array-in-java) and then play it back using an [AudioTrack](https://developer.android.com/reference/android/media/AudioTrack.html), where you can manually continue to provide the correct bytes[] to playback. This will be very complicated and I cannot make any promises if that will work... Personally I would either increase your min SDK level, or accept the issue on those 4year old devices. – Jeroen Mols Nov 25 '15 at 14:58
  • Thanks @jmols, the problem laid out in my question though is that OGG files don't loop back to the start - on ***any*** device, new or old, Gingerbread or Lollipop (see the 4th paragraph above), I do have a problem looping MP3s as mentioned, but what I'm asking about here is specifically the issue with looping OGG Vorbis files. – Zippy Nov 30 '15 at 23:40
  • @csenga, thanks, I will look into your suggestion. – Zippy Nov 30 '15 at 23:42
  • Hey, did you ever solve this? – Russ Wheeler Mar 07 '17 at 01:14
  • Unfortunately not @RussWheeler. Currently (for me anyway) .OGG files are no good for looping, so I use MP3s, – Zippy Mar 16 '17 at 18:48
  • That's odd. I actually got it working with ogg following another so post http://stackoverflow.com/questions/21247670/android-mediaplayer-loop-has-gaps-even-with-ogg-format/33274466?noredirect=1#comment72670235_33274466 – Russ Wheeler Mar 16 '17 at 19:57
  • My problem isn't that there is gap when looping ogg files, quite the opposite in fact- when it loops, it loops back to a point *after* the start. I will look at this answer though to see if it could help. – Zippy Mar 18 '17 at 21:34

0 Answers0