Cocos2d-x Background Music Not Supporting in Android 5.0

Every version of Cocos2d-x that I have tested (2.1 - 3.2) freezes on Android 5.0 when using CocosDension or MediaPlayer to play background music.

I downloaded a number of applications from the Showcase forum, as well as my own games, and every one of them freezes on load or anytime music is played.

I’m trying to bring Google’s attention to this critical issue, and if you have time please help me by testing your applications on Android 5.0 and filing bugs for issues you find.

For the MediaPlayer issue, please Star my ticket and post if your game freezes (or uses MediaPlayer in any way.)

https://code.google.com/p/android-developer-preview/issues/detail?id=1787

Really need help on this ticket.

If you are using Cocos2d-x and deploying to Android (or planning to deploy to Android) you may be at risk of loosing ALL background music or your application may simply freeze and not work.

Google has said they are planning to release in one week. If we do not get traction on this ticket with Google, we all suffer.

Please help!

Does the new AudioEngine in Cocos 3.3 use MediaPlayer? It looks like it might use OpenSL ES, not that really solves the problem of existing apps, but the upcoming version might work properly.

That very well may be true, and would definitely be a fix for people who:

  1. Will be using only 3.3 by November 3, 2014
  2. Do not need to support older versions of Android OS
  3. Are confident that the regression in MediaPlayer won’t effect ES, AudioTrack or SoundPool
  4. Are not concerned about other Cocos2d-x games/developers

Keep in mind that OpenSL ES is not new hardware, it is just another sound library. If platform devs use the existing native code to implement the new OpenES API (which it appears they are) then OpenES will have the exact same bugs as MediaPlayer/AudioTrack/SoundPool.

I have modified this.
I did the English do not ask the answer.

Cocos2dxMusic.java

public void playBackgroundMusic(final String pPath, final boolean isLoop) {

	if (this.mCurrentPath == null) {

		// it is the first time to play background music or end() was called
		this.mBackgroundMediaPlayer = this.createMediaplayer(pPath);
		this.mCurrentPath = pPath;
	} else {

		//if (!this.mCurrentPath.equals(pPath)) { //-removed
			// play new background music

			// release old resource and create a new one
			if (this.mBackgroundMediaPlayer != null) {
				this.mBackgroundMediaPlayer.release();
			}

			this.mBackgroundMediaPlayer = this.createMediaplayer(pPath);

			// record the path
			this.mCurrentPath = pPath;
		//} //-removed
	}


	if (this.mBackgroundMediaPlayer == null) {
		Log.e(Cocos2dxMusic.TAG, "playBackgroundMusic: background media player is null");
	} else {
		// if the music is playing or paused, stop it
		// this.mBackgroundMediaPlayer.stop(); //-removed

		this.mBackgroundMediaPlayer.setLooping(isLoop);

		try {
			//this.mBackgroundMediaPlayer.prepare();  //-removed
			//this.mBackgroundMediaPlayer.seekTo(0); //-removed
			//this.mBackgroundMediaPlayer.start(); //-removed


			this.mBackgroundMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
				@Override
				public void onPrepared(MediaPlayer mp) {	    	
					mBackgroundMediaPlayer.seekTo(0);
					mBackgroundMediaPlayer.start();
				}
			});

			this.mBackgroundMediaPlayer.prepareAsync();

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

private MediaPlayer createMediaplayer(final String pPath) 
{
		MediaPlayer mediaPlayer = new MediaPlayer();

		mediaPlayer.reset();


		try {
			if (pPath.startsWith("/")) {
				final FileInputStream fis = new FileInputStream(pPath);
				mediaPlayer.setDataSource(fis.getFD());
				fis.close();
			} else {
				final AssetFileDescriptor assetFileDescritor = this.mContext.getAssets().openFd(pPath);
				mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(), assetFileDescritor.getStartOffset(), assetFileDescritor.getLength());
			}


			//mediaPlayer.prepare(); //-removed

			mediaPlayer.setVolume(this.mLeftVolume, this.mRightVolume);
		} catch (final Exception e) {

			mediaPlayer = null;

		}



		return mediaPlayer;
}

Hey all… I have Android 5.0 on Nexus 5, it doesn’t freeze with Cocos 3.2, but it’s mute. I’ve tried @turncross method without success, I’ve also tried setting the stream to music, anyway, nada, nicht, null. Tried WAV, M4A, MP3 and OGG.

You can apply my patch, or work backwards from Google’s suggestion on the bug.

Hi all,

It is fixed in v2.2.6 https://github.com/cocos2d/cocos2d-x/issues/9336, and the fixed codes is included since v3.3 https://github.com/cocos2d/cocos2d-x/issues/8962.