diff --git a/cocos/2d/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java b/cocos/2d/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java index c746a357ce..4695317696 100644 --- a/cocos/2d/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java +++ b/cocos/2d/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java @@ -292,6 +292,16 @@ public class Cocos2dxHelper { } } + public static void onEnterBackground() { + sCocos2dSound.onEnterBackground(); + sCocos2dMusic.onEnterBackground(); + } + + public static void onEnterForeground() { + sCocos2dSound.onEnterForeground(); + sCocos2dMusic.onEnterForeground(); + } + public static void terminateProcess() { android.os.Process.killProcess(android.os.Process.myPid()); } diff --git a/cocos/2d/platform/android/java/src/org/cocos2dx/lib/Cocos2dxMusic.java b/cocos/2d/platform/android/java/src/org/cocos2dx/lib/Cocos2dxMusic.java index 41793e24a9..386a75fee4 100644 --- a/cocos/2d/platform/android/java/src/org/cocos2dx/lib/Cocos2dxMusic.java +++ b/cocos/2d/platform/android/java/src/org/cocos2dx/lib/Cocos2dxMusic.java @@ -47,7 +47,8 @@ public class Cocos2dxMusic { private MediaPlayer mBackgroundMediaPlayer; private float mLeftVolume; private float mRightVolume; - private boolean mPaused; + 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; // =========================================================== @@ -142,6 +143,7 @@ public class Cocos2dxMusic { if (this.mBackgroundMediaPlayer != null && this.mBackgroundMediaPlayer.isPlaying()) { this.mBackgroundMediaPlayer.pause(); this.mPaused = true; + this.mManualPaused = true; } } @@ -149,6 +151,7 @@ public class Cocos2dxMusic { if (this.mBackgroundMediaPlayer != null && this.mPaused) { this.mBackgroundMediaPlayer.start(); this.mPaused = false; + this.mManualPaused = false; } } @@ -211,6 +214,22 @@ public class Cocos2dxMusic { } } + 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; diff --git a/cocos/2d/platform/android/java/src/org/cocos2dx/lib/Cocos2dxRenderer.java b/cocos/2d/platform/android/java/src/org/cocos2dx/lib/Cocos2dxRenderer.java index eb1d5d0853..a40d06019e 100644 --- a/cocos/2d/platform/android/java/src/org/cocos2dx/lib/Cocos2dxRenderer.java +++ b/cocos/2d/platform/android/java/src/org/cocos2dx/lib/Cocos2dxRenderer.java @@ -28,6 +28,7 @@ import javax.microedition.khronos.opengles.GL10; import android.opengl.GLSurfaceView; +import org.cocos2dx.lib.Cocos2dxHelper; public class Cocos2dxRenderer implements GLSurfaceView.Renderer { // =========================================================== // Constants @@ -144,10 +145,12 @@ public class Cocos2dxRenderer implements GLSurfaceView.Renderer { } public void handleOnPause() { + Cocos2dxHelper.onEnterBackground(); Cocos2dxRenderer.nativeOnPause(); } public void handleOnResume() { + Cocos2dxHelper.onEnterForeground(); Cocos2dxRenderer.nativeOnResume(); } diff --git a/cocos/2d/platform/android/java/src/org/cocos2dx/lib/Cocos2dxSound.java b/cocos/2d/platform/android/java/src/org/cocos2dx/lib/Cocos2dxSound.java index 48da7e8c44..297d148a86 100644 --- a/cocos/2d/platform/android/java/src/org/cocos2dx/lib/Cocos2dxSound.java +++ b/cocos/2d/platform/android/java/src/org/cocos2dx/lib/Cocos2dxSound.java @@ -129,8 +129,8 @@ public class Cocos2dxSound { // stop effects final ArrayList streamIDs = this.mPathStreamIDsMap.get(pPath); if (streamIDs != null) { - for (final Integer pStreamID : streamIDs) { - this.mSoundPool.stop(pStreamID); + for (final Integer steamID : streamIDs) { + this.mSoundPool.stop(steamID); } } this.mPathStreamIDsMap.remove(pPath); @@ -180,28 +180,36 @@ public class Cocos2dxSound { return streamID; } - public void stopEffect(final int pStreamID) { - this.mSoundPool.stop(pStreamID); + public void stopEffect(final int steamID) { + this.mSoundPool.stop(steamID); // remove record for (final String pPath : this.mPathStreamIDsMap.keySet()) { - if (this.mPathStreamIDsMap.get(pPath).contains(pStreamID)) { - this.mPathStreamIDsMap.get(pPath).remove(this.mPathStreamIDsMap.get(pPath).indexOf(pStreamID)); + if (this.mPathStreamIDsMap.get(pPath).contains(steamID)) { + this.mPathStreamIDsMap.get(pPath).remove(this.mPathStreamIDsMap.get(pPath).indexOf(steamID)); break; } } } - public void pauseEffect(final int pStreamID) { - this.mSoundPool.pause(pStreamID); + public void pauseEffect(final int steamID) { + this.mSoundPool.pause(steamID); } - public void resumeEffect(final int pStreamID) { - this.mSoundPool.resume(pStreamID); + public void resumeEffect(final int steamID) { + this.mSoundPool.resume(steamID); } public void pauseAllEffects() { - this.mSoundPool.autoPause(); + if (!this.mPathStreamIDsMap.isEmpty()) { + final Iterator>> iter = this.mPathStreamIDsMap.entrySet().iterator(); + while (iter.hasNext()) { + final Entry> entry = iter.next(); + for (final int steamID : entry.getValue()) { + this.mSoundPool.pause(steamID); + } + } + } } public void resumeAllEffects() { @@ -211,8 +219,8 @@ public class Cocos2dxSound { final Iterator>> iter = this.mPathStreamIDsMap.entrySet().iterator(); while (iter.hasNext()) { final Entry> entry = iter.next(); - for (final int pStreamID : entry.getValue()) { - this.mSoundPool.resume(pStreamID); + for (final int steamID : entry.getValue()) { + this.mSoundPool.resume(steamID); } } } @@ -225,8 +233,8 @@ public class Cocos2dxSound { final Iterator iter = this.mPathStreamIDsMap.entrySet().iterator(); while (iter.hasNext()) { final Map.Entry> entry = (Map.Entry>) iter.next(); - for (final int pStreamID : entry.getValue()) { - this.mSoundPool.stop(pStreamID); + for (final int steamID : entry.getValue()) { + this.mSoundPool.stop(steamID); } } } @@ -255,8 +263,8 @@ public class Cocos2dxSound { final Iterator>> iter = this.mPathStreamIDsMap.entrySet().iterator(); while (iter.hasNext()) { final Entry> entry = iter.next(); - for (final int pStreamID : entry.getValue()) { - this.mSoundPool.setVolume(pStreamID, this.mLeftVolume, this.mRightVolume); + for (final int steamID : entry.getValue()) { + this.mSoundPool.setVolume(steamID, this.mLeftVolume, this.mRightVolume); } } } @@ -319,6 +327,14 @@ public class Cocos2dxSound { return streamID; } + public void onEnterBackground(){ + this.mSoundPool.autoPause(); + } + + public void onEnterForeground(){ + this.mSoundPool.autoResume(); + } + // =========================================================== // Inner and Anonymous Classes // ===========================================================