Merge pull request #15724 from dumganhar/catch-exception

Try catch IllegalStateException in Cocos2dxMusic.java.
This commit is contained in:
minggo 2016-05-26 11:20:14 +08:00
commit 1f2dea7974
2 changed files with 46 additions and 33 deletions

View File

@ -29,9 +29,6 @@ import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.util.Log;
import android.content.res.AssetFileDescriptor;
import com.android.vending.expansion.zipfile.APKExpansionSupport;
import com.android.vending.expansion.zipfile.ZipResourceFile;
import java.io.FileInputStream;
@ -86,7 +83,7 @@ public class Cocos2dxMusic {
this.mBackgroundMediaPlayer.release();
}
this.mBackgroundMediaPlayer = this.createMediaplayer(path);
this.mBackgroundMediaPlayer = this.createMediaPlayer(path);
// record the path
this.mCurrentPath = path;
@ -96,7 +93,7 @@ public class Cocos2dxMusic {
public void playBackgroundMusic(final String path, final boolean isLoop) {
if (mCurrentPath == null) {
// it is the first time to play background music or end() was called
mBackgroundMediaPlayer = createMediaplayer(path);
mBackgroundMediaPlayer = createMediaPlayer(path);
mCurrentPath = path;
} else {
if (!mCurrentPath.equals(path)) {
@ -106,7 +103,7 @@ public class Cocos2dxMusic {
if (mBackgroundMediaPlayer != null) {
mBackgroundMediaPlayer.release();
}
mBackgroundMediaPlayer = createMediaplayer(path);
mBackgroundMediaPlayer = createMediaPlayer(path);
// record the path
mCurrentPath = path;
@ -138,7 +135,7 @@ public class Cocos2dxMusic {
public void stopBackgroundMusic() {
if (this.mBackgroundMediaPlayer != null) {
mBackgroundMediaPlayer.release();
mBackgroundMediaPlayer = createMediaplayer(mCurrentPath);
mBackgroundMediaPlayer = createMediaPlayer(mCurrentPath);
/**
* should set the state, if not, the following sequence will be error
@ -149,18 +146,26 @@ public class Cocos2dxMusic {
}
public void pauseBackgroundMusic() {
if (this.mBackgroundMediaPlayer != null && this.mBackgroundMediaPlayer.isPlaying()) {
this.mBackgroundMediaPlayer.pause();
this.mPaused = true;
this.mManualPaused = true;
try {
if (this.mBackgroundMediaPlayer != null && this.mBackgroundMediaPlayer.isPlaying()) {
this.mBackgroundMediaPlayer.pause();
this.mPaused = true;
this.mManualPaused = true;
}
} catch (IllegalStateException e) {
Log.e(TAG, "pauseBackgroundMusic, IllegalStateException was triggered!");
}
}
public void resumeBackgroundMusic() {
if (this.mBackgroundMediaPlayer != null && this.mPaused) {
this.mBackgroundMediaPlayer.start();
this.mPaused = false;
this.mManualPaused = false;
try {
if (this.mBackgroundMediaPlayer != null && this.mPaused) {
this.mBackgroundMediaPlayer.start();
this.mPaused = false;
this.mManualPaused = false;
}
} catch (IllegalStateException e) {
Log.e(TAG, "resumeBackgroundMusic, IllegalStateException was triggered!");
}
}
@ -172,11 +177,14 @@ public class Cocos2dxMusic {
public boolean isBackgroundMusicPlaying() {
boolean ret = false;
if (this.mBackgroundMediaPlayer == null) {
ret = false;
} else {
ret = this.mBackgroundMediaPlayer.isPlaying();
try {
if (this.mBackgroundMediaPlayer == null) {
ret = false;
} else {
ret = this.mBackgroundMediaPlayer.isPlaying();
}
} catch (IllegalStateException e) {
Log.e(TAG, "isBackgroundMusicPlaying, IllegalStateException was triggered!");
}
return ret;
@ -214,18 +222,26 @@ public class Cocos2dxMusic {
}
public void onEnterBackground(){
if (this.mBackgroundMediaPlayer != null && this.mBackgroundMediaPlayer.isPlaying()) {
this.mBackgroundMediaPlayer.pause();
this.mPaused = true;
try {
if (this.mBackgroundMediaPlayer != null && this.mBackgroundMediaPlayer.isPlaying()) {
this.mBackgroundMediaPlayer.pause();
this.mPaused = true;
}
} catch (IllegalStateException e) {
Log.e(TAG, "onEnterBackground, IllegalStateException was triggered!");
}
}
public void onEnterForeground(){
if(!this.mManualPaused){
if (this.mBackgroundMediaPlayer != null && this.mPaused) {
this.mBackgroundMediaPlayer.start();
this.mPaused = false;
try {
if (!this.mManualPaused) {
if (this.mBackgroundMediaPlayer != null && this.mPaused) {
this.mBackgroundMediaPlayer.start();
this.mPaused = false;
}
}
} catch (IllegalStateException e) {
Log.e(TAG, "onEnterForeground, IllegalStateException was triggered!");
}
}
@ -238,13 +254,12 @@ public class Cocos2dxMusic {
}
/**
* create mediaplayer for music
* create MediaPlayer for music
*
* @param pPath
* the pPath relative to assets
* @param path The path relative to assets
* @return
*/
private MediaPlayer createMediaplayer(final String path) {
private MediaPlayer createMediaPlayer(final String path) {
MediaPlayer mediaPlayer = new MediaPlayer();
try {

View File

@ -29,8 +29,6 @@ import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;
import android.content.res.AssetFileDescriptor;
import com.android.vending.expansion.zipfile.APKExpansionSupport;
import com.android.vending.expansion.zipfile.ZipResourceFile;
import java.util.ArrayList;
import java.util.HashMap;