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

AudioEngine:Fixed program may freeze if AudioEngine::stop or AudioEngine::stopAll() is invoked frequently on Android. #11762

Merged
merged 2 commits into from
May 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions cocos/audio/android/AudioEngine-inl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
using namespace cocos2d;
using namespace cocos2d::experimental;

#define DELAY_TIME_TO_REMOVE 0.5f

void PlayOverEvent(SLPlayItf caller, void* context, SLuint32 playEvent)
{
if (context && playEvent == SL_PLAYEVENT_HEADATEND)
Expand All @@ -67,6 +69,7 @@ AudioPlayer::AudioPlayer()
, _playOver(false)
, _loop(false)
, _assetFd(0)
, _delayTimeToRemove(-1.f)
{

}
Expand Down Expand Up @@ -276,18 +279,32 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume

void AudioEngineImpl::update(float dt)
{
AudioPlayer* player = nullptr;

auto itend = _audioPlayers.end();
for (auto iter = _audioPlayers.begin(); iter != itend; ++iter)
for (auto iter = _audioPlayers.begin(); iter != itend; )
{
if(iter->second._playOver)
player = &(iter->second);
if (player->_playOver)
{
if (iter->second._finishCallback)
iter->second._finishCallback(iter->second._audioID, *AudioEngine::_audioIDInfoMap[iter->second._audioID].filePath);
if (player->_finishCallback)
player->_finishCallback(player->_audioID, *AudioEngine::_audioIDInfoMap[player->_audioID].filePath);

AudioEngine::remove(iter->second._audioID);
_audioPlayers.erase(iter);
break;
AudioEngine::remove(player->_audioID);
iter = _audioPlayers.erase(iter);
continue;
}
else if (player->_delayTimeToRemove > 0.f)
{
player->_delayTimeToRemove -= dt;
if (player->_delayTimeToRemove < 0.f)
{
iter = _audioPlayers.erase(iter);
continue;
}
}

++iter;
}

if(_audioPlayers.empty()){
Expand Down Expand Up @@ -348,17 +365,27 @@ void AudioEngineImpl::stop(int audioID)
log("%s error:%u",__func__, result);
}

_audioPlayers.erase(audioID);
/*If destroy openSL object immediately,it may cause dead lock.
*It's a system issue.For more information:
* https://github.com/cocos2d/cocos2d-x/issues/11697
* https://groups.google.com/forum/#!msg/android-ndk/zANdS2n2cQI/AT6q1F3nNGIJ
*/
player._delayTimeToRemove = DELAY_TIME_TO_REMOVE;
//_audioPlayers.erase(audioID);
}

void AudioEngineImpl::stopAll()
{
auto itEnd = _audioPlayers.end();
for (auto it = _audioPlayers.begin(); it != itEnd; ++it)
{
auto result = (*it->second._fdPlayerPlay)->SetPlayState(it->second._fdPlayerPlay, SL_PLAYSTATE_STOPPED);
(*it->second._fdPlayerPlay)->SetPlayState(it->second._fdPlayerPlay, SL_PLAYSTATE_STOPPED);
if (it->second._delayTimeToRemove < 0.f)
{
//If destroy openSL object immediately,it may cause dead lock.
it->second._delayTimeToRemove = DELAY_TIME_TO_REMOVE;
}
}
_audioPlayers.clear();
}

float AudioEngineImpl::getDuration(int audioID)
Expand Down
1 change: 1 addition & 0 deletions cocos/audio/android/AudioEngine-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class AudioPlayer
float _duration;
int _audioID;
int _assetFd;
float _delayTimeToRemove;

std::function<void (int, const std::string &)> _finishCallback;

Expand Down