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

View File

@ -29,8 +29,6 @@ import android.media.AudioManager;
import android.media.SoundPool; import android.media.SoundPool;
import android.util.Log; import android.util.Log;
import android.content.res.AssetFileDescriptor; 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.ArrayList;
import java.util.HashMap; import java.util.HashMap;