android5下cocos2dx不能播放背景音乐

."should have subtitile controller already set"

http://discuss.cocos2d-x.org/t/dont-work-android-5-0-lollipop-preview/17794/3

10-20 20:15:51.812: E/MediaPlayer(12998): Should have subtitle controller already set
10-20 20:15:51.812: E/MediaPlayer(12998): Should have subtitle controller already set

http://discuss.cocos2d-x.org/t/android-5-0-lollipop-freezes-with-cocos2dx-3-2-version-l-api-21/17811

解决代码

https://github.com/Dhilan007/cocos2d-x/blob/76c9accec9b8c3884f9ddec83be44df5d828605b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxMusic.java



不过我没搞定, 没觉得能播放, 然后我把背景音乐改成ogg的

于是

可以播了....

为啥, 效果音mp3可以播放, 背景音不行......

再后来用ogg, 又不行了, 后来发现不能预加载背景音乐, 只要预加载就不会播放

另外也不能重复播放那个背景音乐, 否则无声


似乎是这上面的代码出了问题

try {
				// if the music is playing or paused, stop it
				if (mPaused) {
					mBackgroundMediaPlayer.seekTo(0);
					this.mBackgroundMediaPlayer.setLooping(isLoop);
					this.mBackgroundMediaPlayer.start();
				} else if (mBackgroundMediaPlayer.isPlaying()) {
					mBackgroundMediaPlayer.seekTo(0);
					this.mBackgroundMediaPlayer.setLooping(isLoop);
				} else {
					this.mBackgroundMediaPlayer.setLooping(isLoop);
					this.mBackgroundMediaPlayer.start();
					this.mPaused = false;
				}
			} catch (final Exception e) {
				Log.e(Cocos2dxMusic.TAG, "playBackgroundMusic: error state");
			}

暂时的解决方法是, 如果已经播放了当前的背景音乐, 而有从心播放, 就像更换其他背景音乐一样, 销毁并重建

/****************************************************************************
 Copyright (c) 2010-2012 cocos2d-x.org
 Copyright (c) 2013-2014 Chukong Technologies Inc.
 
 http://www.cocos2d-x.org
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

package org.cocos2dx.lib;

import java.io.FileInputStream;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.util.Log;

public class Cocos2dxMusic {
	// ===========================================================
	// Constants
	// ===========================================================
	
	private static final String TAG = Cocos2dxMusic.class.getSimpleName();
	
	// ===========================================================
	// Fields
	// ===========================================================
	
	private final Context mContext;
	private MediaPlayer mBackgroundMediaPlayer;
	private float mLeftVolume;
	private float mRightVolume;
	private boolean mPaused;// whether music is paused state.
	private boolean mManualPaused = false;// whether music is paused manually before the program is switched to the background.
	private String mCurrentPath;
	
	// ===========================================================
	// Constructors
	// ===========================================================
	
	public Cocos2dxMusic(final Context pContext) {
		this.mContext = pContext;
		
		this.initData();
	}
	
	// ===========================================================
	// Getter & Setter
	// ===========================================================
	
	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
	
	// ===========================================================
	// Methods
	// ===========================================================
	
	public void preloadBackgroundMusic(final String pPath) {
		if ((this.mCurrentPath == null) || (!this.mCurrentPath.equals(pPath))) {
			// preload 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;
		}
	}
	
	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)) {
				// 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;
//			}
		}
		
		if (this.mBackgroundMediaPlayer == null) {
			Log.e(Cocos2dxMusic.TAG, "playBackgroundMusic: background media player is null");
		} else {
			try {
				// if the music is playing or paused, stop it
				if (mPaused) {
					mBackgroundMediaPlayer.seekTo(0);
					this.mBackgroundMediaPlayer.setLooping(isLoop);
					this.mBackgroundMediaPlayer.start();
				} else if (mBackgroundMediaPlayer.isPlaying()) {
					mBackgroundMediaPlayer.seekTo(0);
					this.mBackgroundMediaPlayer.setLooping(isLoop);
				} else {
					this.mBackgroundMediaPlayer.setLooping(isLoop);
					this.mBackgroundMediaPlayer.start();
					this.mPaused = false;
				}
			} catch (final Exception e) {
				Log.e(Cocos2dxMusic.TAG, "playBackgroundMusic: error state");
			}
		}
	}
	
	public void stopBackgroundMusic() {
		if (this.mBackgroundMediaPlayer != null) {
			this.mBackgroundMediaPlayer.stop();
			
			// should set the state, if not, the following sequence will be error
			// play -> pause -> stop -> resume
			this.mPaused = false;
		}
	}
	
	public void pauseBackgroundMusic() {
		if (this.mBackgroundMediaPlayer != null && this.mBackgroundMediaPlayer.isPlaying()) {
			this.mBackgroundMediaPlayer.pause();
			this.mPaused = true;
			this.mManualPaused = true;
		}
	}
	
	public void resumeBackgroundMusic() {
		if (this.mBackgroundMediaPlayer != null && this.mPaused) {
			this.mBackgroundMediaPlayer.start();
			this.mPaused = false;
			this.mManualPaused = false;
		}
	}
	
	public void rewindBackgroundMusic() {
		if (this.mBackgroundMediaPlayer != null) {
			this.mBackgroundMediaPlayer.stop();
			
			try {
				this.mBackgroundMediaPlayer.prepare();
				this.mBackgroundMediaPlayer.seekTo(0);
				this.mBackgroundMediaPlayer.start();
				
				this.mPaused = false;
			} catch (final Exception e) {
				Log.e(Cocos2dxMusic.TAG, "rewindBackgroundMusic: error state");
			}
		}
	}
	
	public boolean isBackgroundMusicPlaying() {
		boolean ret = false;
		
		if (this.mBackgroundMediaPlayer == null) {
			ret = false;
		} else {
			ret = this.mBackgroundMediaPlayer.isPlaying();
		}
		
		return ret;
	}
	
	public void end() {
		if (this.mBackgroundMediaPlayer != null) {
			this.mBackgroundMediaPlayer.release();
		}
		
		this.initData();
	}
	
	public float getBackgroundVolume() {
		if (this.mBackgroundMediaPlayer != null) {
			return (this.mLeftVolume + this.mRightVolume) / 2;
		} else {
			return 0.0f;
		}
	}
	
	public void setBackgroundVolume(float pVolume) {
		if (pVolume < 0.0f) {
			pVolume = 0.0f;
		}
		
		if (pVolume > 1.0f) {
			pVolume = 1.0f;
		}
		
		this.mLeftVolume = this.mRightVolume = pVolume;
		if (this.mBackgroundMediaPlayer != null) {
			this.mBackgroundMediaPlayer.setVolume(this.mLeftVolume, this.mRightVolume);
		}
	}
	
	public void onEnterBackground(){
		if (this.mBackgroundMediaPlayer != null && this.mBackgroundMediaPlayer.isPlaying()) {
			this.mBackgroundMediaPlayer.pause();
			this.mPaused = true;
		}
	}
	
	public void onEnterForeground(){
		if(!this.mManualPaused){
			if (this.mBackgroundMediaPlayer != null && this.mPaused) {
				this.mBackgroundMediaPlayer.start();
				this.mPaused = false;
			}
		}
	}
	
	private void initData() {
		this.mLeftVolume = 0.5f;
		this.mRightVolume = 0.5f;
		this.mBackgroundMediaPlayer = null;
		this.mPaused = false;
		this.mCurrentPath = null;
	}
	
	/**
	 * create mediaplayer for music
	 *
	 * @param pPath
	 *            the pPath relative to assets
	 * @return
	 */
	private MediaPlayer createMediaplayer(final String pPath) {
		MediaPlayer mediaPlayer = new MediaPlayer();
		mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		
		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();
			
			mediaPlayer.setVolume(this.mLeftVolume, this.mRightVolume);
		} catch (final Exception e) {
			mediaPlayer = null;
			Log.e(Cocos2dxMusic.TAG, "error: " + e.getMessage(), e);
		}
		
		return mediaPlayer;
	}
	
	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值