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();
}
}