iphone真机先home键然后恢复进入app后的音效问题

http://forum.cocos.com/t/cocos2d-x-2-x-bug/4187
这个历史帖子也有这个问题。

我的问题描述:在app内点击暂停图标按钮后,暂停了动画、背景音乐和音效的播放,如果此时按home键,然后再重新进入app,动画和背景音乐可以播放,但是音效无法播放(偶尔会播放)。再重复一次后,音效就又可以播出了。

这是什么原因呢?
通过在update函数中检测暂停按钮的状态,当切换回来后,调用如下函数 :
cc.director.resume();
cc.audioEngine.resumeMusic();
cc.audioEngine.resumeAllEffects();
仍然无法播放音效。

creator1.2.2beta2

ios的appdelegate.mm的代码片段如下:

// This function will be called when the app is inactive. When comes a phone call,it’s be invoked too
void AppDelegate::applicationDidEnterBackground()
{
auto director = Director::getInstance();
director->stopAnimation();
director->getEventDispatcher()->dispatchCustomEvent(“game_on_hide”);
SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
SimpleAudioEngine::getInstance()->pauseAllEffects();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
auto director = Director::getInstance();
director->startAnimation();
director->getEventDispatcher()->dispatchCustomEvent(“game_on_show”);
SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
SimpleAudioEngine::getInstance()->resumeAllEffects();
}

app内点击暂停按钮,点击home键,再回到app:
1)安卓播放声效和背景音乐,动画暂停。
2)ios背景音乐和动画,声效不播放。再点击home,然后回到app声效就恢复了。

app内的按钮点击暂停:
cc.director.pause();
cc.audioEngine.pauseMusic();
cc.audioEngine.pauseAllEffects();

我这边也有同样的问题

朋友你有什么好的思路么?
我心思监听EVENT_HIDE事件,当切换到后台的时候,如果游戏暂停的话,就把暂停解开,这样就和游戏无暂停一样了,只是切回到游戏的时候就不是暂停状态了,但应该可以保证背景音乐、声效和动画是一致的状态,这个思路我还没试呢,准备周末试一下。

(1)js端:
cc.game.on(cc.game.EVENT_HIDE, function () {
console.log(’####切出游戏####’);
if(cc.director.isPaused()){
cc.find(‘Game/pause’).getComponent(cc.Button).interactable=true;
cc.director.resume();
cc.audioEngine.resumeMusic();
cc.audioEngine.resumeAllEffects();
}
});
(2)iOS原生端:
void AppDelegate::applicationDidEnterBackground()
{
NSLog(@"###切出游戏iOS端###");
auto director = Director::getInstance();
director->stopAnimation();
director->getEventDispatcher()->dispatchCustomEvent(“game_on_hide”);
SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
SimpleAudioEngine::getInstance()->pauseAllEffects();
}
日志如下:
###切出游戏iOS端###
####切出游戏####

为什么(2)在(1)前边执行呢,是因为(2)在(1)之前注册的么?
如果(1),(2)的顺序执行就应该可以达到效果,切回后应该就有声效了,现在这个顺序还是么有达到效果。

还有就是通过oc调用js,根据js端的暂停状态触发不同的函数

这个派发的事件

(1)js端
onOC: function(){
console.log(’####onOC begin####’);
var flag=false;
if(cc.director.isPaused()){
cc.find(‘Game/pause’).getComponent(cc.Button).interactable=true;
cc.director.resume();
cc.audioEngine.resumeMusic();
cc.audioEngine.resumeAllEffects();
flag=true;;
console.log(’####onOC 切出前,暂停恢复####’);
}
console.log(’####onOC end####’);
return flag;
},
(2)oc端
void AppDelegate::applicationDidEnterBackground()
{
NSLog(@"###切出游戏iOS端###");
auto director = Director::getInstance();
/*
director->stopAnimation();
director->getEventDispatcher()->dispatchCustomEvent(“game_on_hide”);
SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
SimpleAudioEngine::getInstance()->pauseAllEffects();
*/
bool paused=ScriptingCore::getInstance()->evalString(“cc.find(‘Game’).getComponent(‘Player’).onOC();”, NULL);
NSLog(@"###iOS paused:###%d",paused);

如上为什么把(2)的原有代码注释后,调用onOC,无论是否点击游戏内暂停,切出游戏时,onOC都是返回true呢?即使不调用onOC,游戏依然可以暂停是为什么呢?被注释的代码没有起到作用呢,当按home键切出的时候还有别的代码执行了游戏的暂停么?
求助官方技术大神@panda @jare

我也遇到过,是cocos-js引擎的问题

你怎么解决的呢?有什么好的思路么?

上面说的这些问题,官方技术大神可以一起讨论下么,或者指点下思路?

切出后的游戏暂停问题,官方技术可以交流指点下么?

感谢官方技术大牛@dumganhar,了解了在AppController.mm里有导演类的暂停和恢复:

  • (void)applicationWillResignActive:(UIApplication )application {
    /

    Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    */
    NSLog(@"###切出游戏iOS端,applicationWillResignActive###");
    bool paused=ScriptingCore::getInstance()->evalString(“cc.find(‘Game’).getComponent(‘Player’).onOC();”, NULL);
    NSLog(@"###iOS paused:###%d",paused);
    cocos2d::Director::getInstance()->pause();
    }

  • (void)applicationDidBecomeActive:(UIApplication )application {
    /

    Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    */
    //调用js
    NSLog(@"###切入游戏iOS端,applicationDidBecomeActive###");
    cocos2d::Director::getInstance()->resume();
    }

通过oc调用js,尝试通过逻辑解决这个切出后,当游戏内有暂停时,再切入的声效播放问题