mirror of https://github.com/axmolengine/axmol.git
[android] fixed #453: cocosdenshion can replace the background music
This commit is contained in:
parent
0b7ca9c865
commit
d8115cacea
|
@ -18,6 +18,7 @@ public class Cocos2dxMusic {
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private MediaPlayer mBackgroundMediaPlayer;
|
private MediaPlayer mBackgroundMediaPlayer;
|
||||||
private boolean mIsPaused;
|
private boolean mIsPaused;
|
||||||
|
private String mCurrentPath;
|
||||||
|
|
||||||
public Cocos2dxMusic(Context context){
|
public Cocos2dxMusic(Context context){
|
||||||
this.mContext = context;
|
this.mContext = context;
|
||||||
|
@ -25,13 +26,30 @@ public class Cocos2dxMusic {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void playBackgroundMusic(String path, boolean isLoop){
|
public void playBackgroundMusic(String path, boolean isLoop){
|
||||||
if (mBackgroundMediaPlayer == null){
|
if (mCurrentPath == null){
|
||||||
mBackgroundMediaPlayer = createMediaplayerFromAssets(path);
|
// it is the first time to play background music
|
||||||
|
// or end() was called
|
||||||
|
mBackgroundMediaPlayer = createMediaplayerFromAssets(path);
|
||||||
|
mCurrentPath = path;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (! mCurrentPath.equals(path)){
|
||||||
|
// play new background music
|
||||||
|
|
||||||
|
// release old resource and create a new one
|
||||||
|
if (mBackgroundMediaPlayer != null){
|
||||||
|
mBackgroundMediaPlayer.release();
|
||||||
|
}
|
||||||
|
mBackgroundMediaPlayer = createMediaplayerFromAssets(path);
|
||||||
|
|
||||||
|
// record the path
|
||||||
|
mCurrentPath = path;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mBackgroundMediaPlayer == null){
|
if (mBackgroundMediaPlayer == null){
|
||||||
Log.e(TAG, "playBackgroundMusic: background media player is null");
|
Log.e(TAG, "playBackgroundMusic: background media player is null");
|
||||||
} else{
|
} else {
|
||||||
// if the music is playing or paused, stop it
|
// if the music is playing or paused, stop it
|
||||||
mBackgroundMediaPlayer.stop();
|
mBackgroundMediaPlayer.stop();
|
||||||
|
|
||||||
|
@ -41,6 +59,8 @@ public class Cocos2dxMusic {
|
||||||
mBackgroundMediaPlayer.prepare();
|
mBackgroundMediaPlayer.prepare();
|
||||||
mBackgroundMediaPlayer.seekTo(0);
|
mBackgroundMediaPlayer.seekTo(0);
|
||||||
mBackgroundMediaPlayer.start();
|
mBackgroundMediaPlayer.start();
|
||||||
|
|
||||||
|
this.mIsPaused = false;
|
||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
Log.e(TAG, "playBackgroundMusic: error state");
|
Log.e(TAG, "playBackgroundMusic: error state");
|
||||||
}
|
}
|
||||||
|
@ -67,6 +87,7 @@ public class Cocos2dxMusic {
|
||||||
public void resumeBackgroundMusic(){
|
public void resumeBackgroundMusic(){
|
||||||
if (mBackgroundMediaPlayer != null && this.mIsPaused){
|
if (mBackgroundMediaPlayer != null && this.mIsPaused){
|
||||||
mBackgroundMediaPlayer.start();
|
mBackgroundMediaPlayer.start();
|
||||||
|
this.mIsPaused = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,9 +99,11 @@ public class Cocos2dxMusic {
|
||||||
mBackgroundMediaPlayer.prepare();
|
mBackgroundMediaPlayer.prepare();
|
||||||
mBackgroundMediaPlayer.seekTo(0);
|
mBackgroundMediaPlayer.seekTo(0);
|
||||||
mBackgroundMediaPlayer.start();
|
mBackgroundMediaPlayer.start();
|
||||||
|
|
||||||
|
this.mIsPaused = false;
|
||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
Log.e(TAG, "rewindBackgroundMusic: error state");
|
Log.e(TAG, "rewindBackgroundMusic: error state");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,15 +120,13 @@ public class Cocos2dxMusic {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void end(){
|
public void end(){
|
||||||
if (mBackgroundMediaPlayer != null)
|
if (mBackgroundMediaPlayer != null){
|
||||||
{
|
|
||||||
mBackgroundMediaPlayer.release();
|
mBackgroundMediaPlayer.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
initData();
|
initData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public float getBackgroundVolume(){
|
public float getBackgroundVolume(){
|
||||||
if (this.mBackgroundMediaPlayer != null){
|
if (this.mBackgroundMediaPlayer != null){
|
||||||
return (this.mLeftVolume + this.mRightVolume) / 2;
|
return (this.mLeftVolume + this.mRightVolume) / 2;
|
||||||
|
@ -126,6 +147,7 @@ public class Cocos2dxMusic {
|
||||||
mRightVolume = 0.5f;
|
mRightVolume = 0.5f;
|
||||||
mBackgroundMediaPlayer = null;
|
mBackgroundMediaPlayer = null;
|
||||||
mIsPaused = false;
|
mIsPaused = false;
|
||||||
|
mCurrentPath = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -146,6 +168,7 @@ public class Cocos2dxMusic {
|
||||||
|
|
||||||
mediaPlayer.setVolume(mLeftVolume, mRightVolume);
|
mediaPlayer.setVolume(mLeftVolume, mRightVolume);
|
||||||
}catch (Exception e) {
|
}catch (Exception e) {
|
||||||
|
mediaPlayer = null;
|
||||||
Log.e(TAG, "error: " + e.getMessage(), e);
|
Log.e(TAG, "error: " + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,7 @@ public class Cocos2dxMusic {
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private MediaPlayer mBackgroundMediaPlayer;
|
private MediaPlayer mBackgroundMediaPlayer;
|
||||||
private boolean mIsPaused;
|
private boolean mIsPaused;
|
||||||
|
private String mCurrentPath;
|
||||||
|
|
||||||
public Cocos2dxMusic(Context context){
|
public Cocos2dxMusic(Context context){
|
||||||
this.mContext = context;
|
this.mContext = context;
|
||||||
|
@ -25,13 +26,30 @@ public class Cocos2dxMusic {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void playBackgroundMusic(String path, boolean isLoop){
|
public void playBackgroundMusic(String path, boolean isLoop){
|
||||||
if (mBackgroundMediaPlayer == null){
|
if (mCurrentPath == null){
|
||||||
mBackgroundMediaPlayer = createMediaplayerFromAssets(path);
|
// it is the first time to play background music
|
||||||
|
// or end() was called
|
||||||
|
mBackgroundMediaPlayer = createMediaplayerFromAssets(path);
|
||||||
|
mCurrentPath = path;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (! mCurrentPath.equals(path)){
|
||||||
|
// play new background music
|
||||||
|
|
||||||
|
// release old resource and create a new one
|
||||||
|
if (mBackgroundMediaPlayer != null){
|
||||||
|
mBackgroundMediaPlayer.release();
|
||||||
|
}
|
||||||
|
mBackgroundMediaPlayer = createMediaplayerFromAssets(path);
|
||||||
|
|
||||||
|
// record the path
|
||||||
|
mCurrentPath = path;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mBackgroundMediaPlayer == null){
|
if (mBackgroundMediaPlayer == null){
|
||||||
Log.e(TAG, "playBackgroundMusic: background media player is null");
|
Log.e(TAG, "playBackgroundMusic: background media player is null");
|
||||||
} else{
|
} else {
|
||||||
// if the music is playing or paused, stop it
|
// if the music is playing or paused, stop it
|
||||||
mBackgroundMediaPlayer.stop();
|
mBackgroundMediaPlayer.stop();
|
||||||
|
|
||||||
|
@ -41,6 +59,8 @@ public class Cocos2dxMusic {
|
||||||
mBackgroundMediaPlayer.prepare();
|
mBackgroundMediaPlayer.prepare();
|
||||||
mBackgroundMediaPlayer.seekTo(0);
|
mBackgroundMediaPlayer.seekTo(0);
|
||||||
mBackgroundMediaPlayer.start();
|
mBackgroundMediaPlayer.start();
|
||||||
|
|
||||||
|
this.mIsPaused = false;
|
||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
Log.e(TAG, "playBackgroundMusic: error state");
|
Log.e(TAG, "playBackgroundMusic: error state");
|
||||||
}
|
}
|
||||||
|
@ -67,6 +87,7 @@ public class Cocos2dxMusic {
|
||||||
public void resumeBackgroundMusic(){
|
public void resumeBackgroundMusic(){
|
||||||
if (mBackgroundMediaPlayer != null && this.mIsPaused){
|
if (mBackgroundMediaPlayer != null && this.mIsPaused){
|
||||||
mBackgroundMediaPlayer.start();
|
mBackgroundMediaPlayer.start();
|
||||||
|
this.mIsPaused = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,9 +99,11 @@ public class Cocos2dxMusic {
|
||||||
mBackgroundMediaPlayer.prepare();
|
mBackgroundMediaPlayer.prepare();
|
||||||
mBackgroundMediaPlayer.seekTo(0);
|
mBackgroundMediaPlayer.seekTo(0);
|
||||||
mBackgroundMediaPlayer.start();
|
mBackgroundMediaPlayer.start();
|
||||||
|
|
||||||
|
this.mIsPaused = false;
|
||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
Log.e(TAG, "rewindBackgroundMusic: error state");
|
Log.e(TAG, "rewindBackgroundMusic: error state");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,15 +120,13 @@ public class Cocos2dxMusic {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void end(){
|
public void end(){
|
||||||
if (mBackgroundMediaPlayer != null)
|
if (mBackgroundMediaPlayer != null){
|
||||||
{
|
|
||||||
mBackgroundMediaPlayer.release();
|
mBackgroundMediaPlayer.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
initData();
|
initData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public float getBackgroundVolume(){
|
public float getBackgroundVolume(){
|
||||||
if (this.mBackgroundMediaPlayer != null){
|
if (this.mBackgroundMediaPlayer != null){
|
||||||
return (this.mLeftVolume + this.mRightVolume) / 2;
|
return (this.mLeftVolume + this.mRightVolume) / 2;
|
||||||
|
@ -126,6 +147,7 @@ public class Cocos2dxMusic {
|
||||||
mRightVolume = 0.5f;
|
mRightVolume = 0.5f;
|
||||||
mBackgroundMediaPlayer = null;
|
mBackgroundMediaPlayer = null;
|
||||||
mIsPaused = false;
|
mIsPaused = false;
|
||||||
|
mCurrentPath = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -146,6 +168,7 @@ public class Cocos2dxMusic {
|
||||||
|
|
||||||
mediaPlayer.setVolume(mLeftVolume, mRightVolume);
|
mediaPlayer.setVolume(mLeftVolume, mRightVolume);
|
||||||
}catch (Exception e) {
|
}catch (Exception e) {
|
||||||
|
mediaPlayer = null;
|
||||||
Log.e(TAG, "error: " + e.getMessage(), e);
|
Log.e(TAG, "error: " + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue