New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix audio engine crash when enter foreground #1930
Conversation
cocos/audio/AudioEngine.cpp
Outdated
@@ -377,6 +377,11 @@ void AudioEngine::remove(int audioID) | |||
} | |||
_audioPathIDMap[*it->second.filePath].remove(audioID); | |||
_audioIDInfoMap.erase(audioID); | |||
// In case audio ended while application enter background | |||
auto breakAudioIt = std::find(_breakAudioID.begin(), _breakAudioID.end(), audioID); |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有问题应该也是挂在 resume里面吧
根本原因是进入后台后,计时器还在调 update,一些 remove 操作会有问题 Android 平台的实现没问题,已经做了判断 |
那如果进入后台,把timer停了呢 |
这个 timer 是整个主循环了,不太确定直接停掉会不会引发其他问题,比较保险的是在 resume 那里做保护 |
不停主循环也行呀,在update里面做一次是否是后台的判断?因为加这个保护很容易出其他问题,一定要从_audioPlayers数组中移除才行,不然,开发者可以用 _audioPlayers[i] -> xxx 都会出错 |
后续可以考虑加一个 isOnBackground 的全局标记。不过这个保护还是有必要加的 |
EventDispatcher 有发送相关的事件,直接监听事件就可以。 |
这个不能保证音频的事件回调在最后一个处理,可能处理完 AudioEngine 的回调之后,其他的回调又更新了相关的 map |
我只是针对这个问题说的。 至于该 issue 本来就应该判断是否是 valid,否则随便传个非法的 id 也是会崩溃。 |
哦哦,那没问题 |
Re: https://github.com/cocos-creator/2d-tasks/issues/2038
changeLog:
进入后台,有可能音效刚好播放结束,刚好被移除了,这时候需要同步 _breakAudioID
防止进入前台后,播放已经移除了的 Audio