mirror of https://github.com/axmolengine/axmol.git
Merge pull request #6626 from Ryeeeeee/v3_pause_sound_background
sounds pause automatically when switching background
This commit is contained in:
commit
d84e26c7b4
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -129,8 +129,8 @@ public class Cocos2dxSound {
|
|||
// stop effects
|
||||
final ArrayList<Integer> 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<Entry<String, ArrayList<Integer>>> iter = this.mPathStreamIDsMap.entrySet().iterator();
|
||||
while (iter.hasNext()) {
|
||||
final Entry<String, ArrayList<Integer>> 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<Entry<String, ArrayList<Integer>>> iter = this.mPathStreamIDsMap.entrySet().iterator();
|
||||
while (iter.hasNext()) {
|
||||
final Entry<String, ArrayList<Integer>> 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<String, ArrayList<Integer>> entry = (Map.Entry<String, ArrayList<Integer>>) 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<Entry<String, ArrayList<Integer>>> iter = this.mPathStreamIDsMap.entrySet().iterator();
|
||||
while (iter.hasNext()) {
|
||||
final Entry<String, ArrayList<Integer>> 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
|
||||
// ===========================================================
|
||||
|
|
Loading…
Reference in New Issue