Cocos2dx v2 Background Music issue with Lollipop update

So we experienced an issue with Lollipop update with Background Music not playing at all.
Rest all the other sounds were playing.

After a few logs i figured there is an issue with the Android Mediaplayer update.
This is a running fix, not a proper fix. So you need it apply it and test otherwise wait for an official fix from google on this.

Go to - cocos2dx/platform/android/java/src/…/…/…/…/Cocos2dxMusic.java

Add a function so as to apply this fix only for those devices running lollipop os.

public boolean isLollipop() {
		Log.d("androidVersion ", String.valueOf(android.os.Build.VERSION.SDK_INT));
	
	if(android.os.Build.VERSION.SDK_INT>=21) {
		return true;
	}
	return false;
}

Make changes in following functions:

public void playBackgroundMusic(final String pPath, final boolean isLoop) {
// if the music is playing or paused, stop it
			if(!isLollipop())
			{
				this.mBackgroundMediaPlayer.stop();
			}

		this.mBackgroundMediaPlayer.setLooping(isLoop);

		try {
			
			if(!isLollipop())
			{
				this.mBackgroundMediaPlayer.prepare();
			}
			
			this.mBackgroundMediaPlayer.seekTo(0);
			this.mBackgroundMediaPlayer.start();

			this.mPaused = false;
		}
}
public void stopBackgroundMusic() {
	if (this.mBackgroundMediaPlayer != null) {
		this.mBackgroundMediaPlayer.stop();

		if(isLollipop())
		{
			this.mBackgroundMediaPlayer.release();
			this.mBackgroundMediaPlayer = null;
			this.mCurrentPath = null;
		}
		
		// should set the state, if not, the following sequence will be error
		// play -> pause -> stop -> resume
		this.mPaused = false;
	}
}

public void rewindBackgroundMusic() {
	if (this.mBackgroundMediaPlayer != null) {
		
		if(!isLollipop())
		{
			this.mBackgroundMediaPlayer.stop();
		}
		
		try {
			
			if(!isLollipop())
			{
				this.mBackgroundMediaPlayer.prepare();
			}
			else
			{
				this.mBackgroundMediaPlayer.pause();
			}
			
			this.mBackgroundMediaPlayer.seekTo(0);
			this.mBackgroundMediaPlayer.start();

			this.mPaused = false;
		} catch (final Exception e) {
			Log.e(Cocos2dxMusic.TAG, "rewindBackgroundMusic: error state");
		}
	}
}
3 Likes

Thanks dude… works great on lollipop. The other fixes failed to load the music one kitkat. :slight_smile:

Thanks, this works well! I also tried pull #9088 first but it did nothing.
It kinda sucks to have checks for the OS version in the code like that, but at least it means I don’t have to re-test my game all over again on every other OS version.