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
Merged

fix audio engine crash when enter foreground #1930

merged 1 commit into from Nov 14, 2019

Conversation

PPpro
Copy link
Contributor

@PPpro PPpro commented Nov 12, 2019

Re: cocos/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
Contributor

@PatriceJiang PatriceJiang Nov 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@PPpro
Copy link
Contributor Author

@PPpro PPpro commented Nov 13, 2019

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

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

@holycanvas
Copy link
Contributor

@holycanvas holycanvas commented Nov 13, 2019

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

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

@PPpro
Copy link
Contributor Author

@PPpro PPpro commented Nov 13, 2019

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

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

@holycanvas
Copy link
Contributor

@holycanvas holycanvas commented Nov 13, 2019

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

@PPpro
Copy link
Contributor Author

@PPpro PPpro commented Nov 13, 2019

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

@minggo
Copy link
Contributor

@minggo minggo commented Nov 13, 2019

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

@PPpro
Copy link
Contributor Author

@PPpro PPpro commented Nov 13, 2019

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

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

@minggo
Copy link
Contributor

@minggo minggo commented Nov 13, 2019

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

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

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

@PPpro
Copy link
Contributor Author

@PPpro PPpro commented Nov 13, 2019

哦哦,那没问题

@holycanvas holycanvas merged commit 4ab07e1 into cocos:v2.2.1-release Nov 14, 2019
1 check passed
@PPpro PPpro deleted the 221_audio branch Nov 14, 2019
@PPpro PPpro mentioned this pull request Oct 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants