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