IOS设备自动锁屏后再次进入游戏音效失效

IOS设备自动锁屏后再次进入游戏音效失效

【问题描述】

cocos2dx版本2.2.3

IOS设备自动锁屏后再次进入游戏音效失效,在ipad上貌似没出现,不过itouch上面基本都会这样

【定位过程】

1、这个问题以前是有处理过的,不过当时没注意记录,想不起怎么回事了

2、尝试走读代码,对cocos2dx IOS部分音频代码真头疼

3、在百度上找不到解决办法,只能向老外求助了。谷歌被封了,问问Bing吧

最后搜到了cocos2dx社区的一个帖子,问题顺利解决

http://discuss.cocos2d-x.org/t/sound-fx-not-playing-after-a-while/623/26
hawkwoodOct '13
I found a possible solution to my issue, it’s a three parter:
1) CocosDenshion should only pause effects/bgm that is currently playing (see step 9 from previous post) https://github.com/cocos2d/cocos2d-x/pull/394659
2) in AppDelegate.cpp don’t pause/resume the background music, let the OS just fade it out. (I realize this is iOS specific so some modifications may need to accomodate other OSs)
3) (optional?) in AppController.mm add pauseAllEffects and resumeAllEffects to applicationWillResignActive and applicationDidBecomeActive, respectively.

【修改处理】
实际测试,只要完成第二点就OK了
步骤1的修改:
4 ????? CocosDenshion/ios/CDAudioManager.m
 @@ -569,6 +569,10 @@ -(void) stopBackgroundMusic
 
 -(void) pauseBackgroundMusic
 {
+    if (![self.backgroundMusic isPlaying]) {
+        return;
+    }
+
     [self.backgroundMusic pause];
 }    
 


 9 ????? CocosDenshion/ios/CocosDenshion.m
 @@ -974,6 +974,15 @@ - (void) pauseSound:(ALuint) sourceId {
   if (!functioning_) {
       return;
   }
+
+  // only pause a sound id that is playing
+  ALint state;
+  alGetSourcei(sourceId, AL_SOURCE_STATE, &state);
+  if (state != AL_PLAYING)
+  {
+    return;
+  }
+
   alSourcePause(sourceId);
   alGetError();//Clear error in case we pause any sounds that couldn't be paused

 }

【总结】

事实再次告诉我们:好记性不如烂笔头

你可能感兴趣的:(问题定位)