Not able to achieve Gapless audio looping so far on Android
From the test that I have done, this solution works fine, over 150 loops with a 13 seconds 160 kbps MP3 without any problem:
public class LoopMediaPlayer {
public static final String TAG = LoopMediaPlayer.class.getSimpleName();
private Context mContext = null;
private int mResId = 0;
private int mCounter = 1;
private MediaPlayer mCurrentPlayer = null;
private MediaPlayer mNextPlayer = null;
public static LoopMediaPlayer create(Context context, int resId) {
return new LoopMediaPlayer(context, resId);
}
private LoopMediaPlayer(Context context, int resId) {
mContext = context;
mResId = resId;
mCurrentPlayer = MediaPlayer.create(mContext, mResId);
mCurrentPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mCurrentPlayer.start();
}
});
createNextMediaPlayer();
}
private void createNextMediaPlayer() {
mNextPlayer = MediaPlayer.create(mContext, mResId);
mCurrentPlayer.setNextMediaPlayer(mNextPlayer);
mCurrentPlayer.setOnCompletionListener(onCompletionListener);
}
private MediaPlayer.OnCompletionListener onCompletionListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
mCurrentPlayer = mNextPlayer;
createNextMediaPlayer();
Log.d(TAG, String.format("Loop #%d", ++mCounter));
}
};
}
To use LoopMediaPlayer
you can just call:
LoopMediaPlayer.create(context, R.raw.sample);
How to loop a sound without gaps in Android?
Try using the .ogg file format for the sound files in your application. I had this same issue, and after a lot of research , i tried the .ogg format and it loops without any gaps in android.
Info about .ogg format: http://en.wikipedia.org/wiki/.ogg
You can easily convert your sound file from .mp3 or what have you, to .ogg using the popular open source app VLC Media Player.
Related Topics
Android: Change Shape Color in Runtime
Disable the Notification Panel from Being Pulled Down
Shadow Effect for a Text in Android
Add a Background Image to Shape in Xml Android
Android - How to Pass Hashmap<String,String> Between Activities
Create and Share a File from Internal Storage
Android - How to Make Slide Menu Like Facebook, Spotify and Google +
Android - Setonclicklistener VS Onclicklistener VS View.Onclicklistener
How to Always Run a Service in the Background
Android Java Lang Runtimeexception Fail to Connect to Camera Service
Linkedlist Put into Intent Extra Gets Recast to Arraylist When Retrieving in Next Activity
Which Correct Flag of Autoconnect in Connectgatt of Ble
How to Verify That an Android APK Is Signed with a Release Certificate
How to Determine the Screen Width in Terms of Dp or Dip at Runtime in Android
In Androidx.Fragment.App.Fragment,Setuservisiblehint()Is Deprecated,And Not Executed,Why