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

Pause all music when the app enters background #1849

Merged
merged 1 commit into from Sep 18, 2019

Conversation

PatriceJiang
Copy link
Contributor

invoke AudioEngine::pauseAll when the app enters background

ref https://github.com/cocos-creator/2d-tasks/issues/1744

invoke AudioEngine::pauseAll when app enter background
@minggo minggo merged commit 632acdf into cocos:v2.2.0 Sep 18, 2019
@@ -79,10 +80,12 @@ bool AppDelegate::applicationDidFinishLaunching()
void AppDelegate::applicationDidEnterBackground()
{
EventDispatcher::dispatchEnterBackgroundEvent();
AudioEngine::pauseAll();
Copy link
Contributor

Choose a reason for hiding this comment

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

这里是不是应该只暂停正在播放的音乐呢? @PatriceJiang
可以看下这个 issue:https://github.com/cocos-creator/2d-tasks/issues/1886

Android 的 AudioEngine 实现好像有一部分重复处理了,这里是不是应该去掉呢

void AudioEngineImpl::onEnterBackground(const CustomEvent& event)
{
    // _audioPlayerProvider->pause() pauses AudioMixer and PcmAudioService,
    // but UrlAudioPlayers could not be paused.
    if (_audioPlayerProvider != nullptr)
    {
        _audioPlayerProvider->pause();
    }

    // pause UrlAudioPlayers which are playing.
    for (auto&& e : _audioPlayers)
    {
        auto player = e.second;
        if (dynamic_cast<UrlAudioPlayer*>(player) != nullptr
            && player->getState() == IAudioPlayer::State::PLAYING)
        {
            _urlAudioPlayersNeedResume.emplace(e.first, player);
            player->pause();
        }
    }
}

void AudioEngineImpl::onEnterForeground(const CustomEvent& event)
{
    // _audioPlayerProvider->resume() resumes AudioMixer and PcmAudioService,
    // but UrlAudioPlayers could not be resumed.
    if (_audioPlayerProvider != nullptr)
    {
        _audioPlayerProvider->resume();
    }

    // resume UrlAudioPlayers
    for (auto&& iter : _urlAudioPlayersNeedResume)
    {
        iter.second->resume();
    }
    _urlAudioPlayersNeedResume.clear();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

是的,参考android 的实现,应该保存被暂停的id在 enter foreground 的时候 resume。
需要加一个接口,返回被暂停的id。

@PatriceJiang PatriceJiang deleted the pause_audio_when_enterbackground branch October 12, 2019 05:34
minggo pushed a commit that referenced this pull request Oct 12, 2019
* Revert "Pause music when app enter background (#1849)"

This reverts commit 632acdf.

* refine pause playing audio when enter background
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

3 participants