From bbedddbf08efac680b7489d08b579dd74fc87513 Mon Sep 17 00:00:00 2001 From: SaxonDruce Date: Thu, 21 Jul 2016 17:41:24 +0800 Subject: [PATCH] Fix bugs with music not resuming when iOS app is reactivated (#16178) * Pause instead of stopping music on resign The [audioSource stop] causes the music to be stopped, and therefore it fails to resume later on a call to [audioSource resume], due to the addition of the if (!stopped) check in resume added in https://github.com/cocos2d/cocos2d-x/commit/26a04b38f2bd879d5705b38f55470f9fd9423df6 * Don't re-pause music that has already been paused In this situation: 1. Start game, music plays 2. Switch to Music app, game music paused, start other music 3. Switch to game, game music not resumed due to other music playing 4. Switch to Music app, stop other music 5. Switch back to game, game music should resume due to no other music playing At step 5 the game music doesn't currently resume. This is because at step 4 when switching to the Music app, the game music gets re-paused (actually isPlaying is false, so systemPaused is set to NO, even though the music *is* still paused). This causes the music to not be resumed at step 5. This change fixes this, by skipping the pause logic if systemPaused is already true. Note that this is dependent on https://github.com/cocos2d/cocos2d-x/pull/16178 * Fix typo in previous fix. --- cocos/audio/ios/CDAudioManager.m | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/cocos/audio/ios/CDAudioManager.m b/cocos/audio/ios/CDAudioManager.m index 23f855e800..c0b41f2ebc 100644 --- a/cocos/audio/ios/CDAudioManager.m +++ b/cocos/audio/ios/CDAudioManager.m @@ -644,15 +644,17 @@ static BOOL configured = FALSE; case kAMRBStopPlay: for( CDLongAudioSource *audioSource in audioSourceChannels) { - if (audioSource.isPlaying) { - audioSource->systemPaused = YES; - audioSource->systemPauseLocation = audioSource.audioSourcePlayer.currentTime; - [audioSource stop]; - } else { - //Music is either paused or stopped, if it is paused it will be restarted - //by OS so we will stop it. - audioSource->systemPaused = NO; - [audioSource stop]; + if (!audioSource->systemPaused) { + if (audioSource.isPlaying) { + audioSource->systemPaused = YES; + audioSource->systemPauseLocation = audioSource.audioSourcePlayer.currentTime; + [audioSource pause]; + } else { + //Music is either paused or stopped, if it is paused it will be restarted + //by OS so we will stop it. + audioSource->systemPaused = NO; + [audioSource stop]; + } } } break;