OGG files won't loop correctly using Android using MediaPlayer
Asked Answered
L

0

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();
    }
}
Leasia answered 16/11, 2015 at 20:57 Comment(9)
Have your tried any alternative method to play your file ? I would try this: github.com/wseemann/FFmpegMediaPlayerSome
According to the official docs, gapless playback was only added in Jellybean: developer.android.com/about/versions/android-4.1.html#Gapless, so I guess you are out of luck.Watch
One (complicated) thing you could try is [decode a Wav file to a byte[] array](#10397772) and then play it back using an AudioTrack, 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.Watch
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.Leasia
@csenga, thanks, I will look into your suggestion.Leasia
Hey, did you ever solve this?Anglesite
Unfortunately not @RussWheeler. Currently (for me anyway) .OGG files are no good for looping, so I use MP3s,Leasia
That's odd. I actually got it working with ogg following another so post #21248170Anglesite
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.Leasia

© 2022 - 2024 — McMap. All rights reserved.