Background music loop on Android

I’m trying to use background music with loop on Android devices. When music restarts there is a small pause both in game and music. I’m using preloadBackgroundMusic, and CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(sound,true).

I have tested with wav and mp3 file formats:
Android 2.3.4: just .wav is ok.
Android 2.3.6: .wav and .mp3 are ok.
Android 5.1: .wav and .mp3 are NOT ok.

I would like to known what is the best solution to avoid these lags (.ogg file format?).

The music’s length is less than 1MB and for iOS and Windows Phone 8.1 everything is ok with .wav files.

I’m using cocos2d-x v3.4 and ndk r9d.

Thanks.

I have the same problem and .ogg does not solve the issue. I think is an open thing and they are working on it without solution currently but I hope I am mistaken or a patch is released soon.

@alexgg Thanks for your feedback. You are right, .ogg files have same behavior of .wav files on my tests. Now, I’m looking for a workaround to solve this problem.

I did it and got a good result:

Edited class “Cocos2dxMusic.java” and added the following code snippets.

Declared the following variables:
private MediaPlayer mNextPlayer;
private int mCounter = 1;

In the method “playBackgroundMusic”, added:
if (isLoop) {
createNextMediaPlayer (path);
}
after each call mBackgroundMediaPlayer.start ();

I commented mBackgroundMediaPlayer.setLooping (isLoop);

Added methods:

private void createNextMediaPlayer (final String path)
{
mNextPlayer = createMediaplayer (path);
mBackgroundMediaPlayer.setNextMediaPlayer (mNextPlayer);
mBackgroundMediaPlayer.setOnCompletionListener (onCompletionListener);
}

private MediaPlayer.OnCompletionListener onCompletionListener = new MediaPlayer.OnCompletionListener ()
{
Override
public void onCompletion (MediaPlayer MediaPlayer) {
mediaPlayer.release ();
mBackgroundMediaPlayer = mNextPlayer;
createNextMediaPlayer (mCurrentPath);
Log.e (Cocos2dxMusic.TAG, String.format (“Loop #% d”, mCounter ++));
}
};