Skip to content
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

Merged
merged 1 commit into from Nov 14, 2019

Conversation

@umbrellaPP
Copy link
Contributor

umbrellaPP commented Nov 12, 2019

Re: cocos-creator/2d-tasks#2038

changeLog:

  • 修复 audioEngine 切换前后台导致的崩溃问题

进入后台,有可能音效刚好播放结束,刚好被移除了,这时候需要同步 _breakAudioID
防止进入前台后,播放已经移除了的 Audio

@@ -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.

Copy link
@PatriceJiang

PatriceJiang Nov 12, 2019

Contributor

onEnterForeground 里面增加保护是不是更合理

 void AudioEngine::onEnterForeground() {
     auto itEnd = _breakAudioID.end();
     for (auto it = _breakAudioID.begin(); it != itEnd; ++it) {
+        if audioID Exists and audioState isPaused and ...
+        {
             AudioEngine::resume(*it);
+        }
     _breakAudioID.clear(); 
 }

其他地方可以不维护 _breakAudioID

This comment has been minimized.

Copy link
@PatriceJiang

PatriceJiang Nov 12, 2019

Contributor

有问题应该也是挂在 resume里面吧

@umbrellaPP umbrellaPP force-pushed the umbrellaPP:221_audio branch from 8630783 to 0cfa936 Nov 13, 2019
@umbrellaPP

This comment has been minimized.

Copy link
Contributor Author

umbrellaPP commented Nov 13, 2019

根本原因是进入后台后,计时器还在调 update,一些 remove 操作会有问题
这个还没什么好的办法解决
暂时先在 pause resume stop 做下空指针判断

Android 平台的实现没问题,已经做了判断

@holycanvas

This comment has been minimized.

Copy link
Contributor

holycanvas commented Nov 13, 2019

根本原因是进入后台后,计时器还在调 update

那如果进入后台,把timer停了呢

@umbrellaPP

This comment has been minimized.

Copy link
Contributor Author

umbrellaPP commented Nov 13, 2019

那如果进入后台,把timer停了呢

这个 timer 是整个主循环了,不太确定直接停掉会不会引发其他问题,比较保险的是在 resume 那里做保护

@holycanvas

This comment has been minimized.

Copy link
Contributor

holycanvas commented Nov 13, 2019

不停主循环也行呀,在update里面做一次是否是后台的判断?因为加这个保护很容易出其他问题,一定要从_audioPlayers数组中移除才行,不然,开发者可以用 _audioPlayers[i] -> xxx 都会出错

@umbrellaPP

This comment has been minimized.

Copy link
Contributor Author

umbrellaPP commented Nov 13, 2019

后续可以考虑加一个 isOnBackground 的全局标记。不过这个保护还是有必要加的

@minggo

This comment has been minimized.

Copy link
Contributor

minggo commented Nov 13, 2019

EventDispatcher 有发送相关的事件,直接监听事件就可以。

@umbrellaPP umbrellaPP force-pushed the umbrellaPP:221_audio branch from 0cfa936 to bed5425 Nov 13, 2019
@umbrellaPP

This comment has been minimized.

Copy link
Contributor Author

umbrellaPP commented Nov 13, 2019

EventDispatcher 有发送相关的事件,直接监听事件就可以。

这个不能保证音频的事件回调在最后一个处理,可能处理完 AudioEngine 的回调之后,其他的回调又更新了相关的 map
AudioEngineImpl 定时器的 update 也会更新 _audioPlayers,而且调用没有先后顺序

@minggo

This comment has been minimized.

Copy link
Contributor

minggo commented Nov 13, 2019

后续可以考虑加一个 isOnBackground 的全局标记。不过这个保护还是有必要加的

我只是针对这个问题说的。

至于该 issue 本来就应该判断是否是 valid,否则随便传个非法的 id 也是会崩溃。

@umbrellaPP

This comment has been minimized.

Copy link
Contributor Author

umbrellaPP commented Nov 13, 2019

哦哦,那没问题

@holycanvas holycanvas merged commit 4ab07e1 into cocos-creator:v2.2.1-release Nov 14, 2019
1 check passed
1 check passed
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@umbrellaPP umbrellaPP deleted the umbrellaPP:221_audio branch Nov 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.