diff --git a/cocos/audio/AudioEngineImpl.cpp b/cocos/audio/AudioEngineImpl.cpp index 2cfef93c2f..538a127e4f 100644 --- a/cocos/audio/AudioEngineImpl.cpp +++ b/cocos/audio/AudioEngineImpl.cpp @@ -41,6 +41,7 @@ #include "base/CCScheduler.h" #include "platform/CCFileUtils.h" #include "audio/include/AudioDecoderManager.h" +#include "audio/include/AudioPlayer.h" using namespace cocos2d; @@ -494,6 +495,7 @@ void AudioEngineImpl::_updateLocked(float dt) finishCallback(audioID, filePath); //FIXME: callback will delay 50ms }); #endif + player->setCache(nullptr); // it's safe for player didn't free audio cache } delete player; _alSourceUsed[alSource] = false; @@ -521,6 +523,10 @@ void AudioEngineImpl::uncache(const std::string &filePath) void AudioEngineImpl::uncacheAll() { + // prevent player hold invalid AudioCache* pointer, since all audio caches purged + for (auto& player : _audioPlayers) + player.second->setCache(nullptr); + _audioCaches.clear(); } diff --git a/cocos/audio/apple/AudioEngineImpl.mm b/cocos/audio/apple/AudioEngineImpl.mm index 641afc383a..0a4136d8bc 100644 --- a/cocos/audio/apple/AudioEngineImpl.mm +++ b/cocos/audio/apple/AudioEngineImpl.mm @@ -751,6 +751,7 @@ void AudioEngineImpl::_updateLocked(float dt) _finishCallbacks.push_back([finishCallback = std::move(player->_finishCallbak), audioID, filePath = std::move(filePath)](){ finishCallback(audioID, filePath); // FIXME: callback will delay 50ms }); + player->setCache(nullptr); // it's safe for player didn't free audio cache } delete player; @@ -778,7 +779,11 @@ void AudioEngineImpl::uncache(const std::string &filePath) } void AudioEngineImpl::uncacheAll() -{ +{ + // prevent player hold invalid AudioCache* pointer, since all audio caches purged + for(auto& player : _audioPlayers) + player.second->setCache(nullptr); + _audioCaches.clear(); } diff --git a/cocos/audio/include/AudioPlayer.h b/cocos/audio/include/AudioPlayer.h index a37eb58520..dd9442df67 100644 --- a/cocos/audio/include/AudioPlayer.h +++ b/cocos/audio/include/AudioPlayer.h @@ -46,6 +46,7 @@ class AudioEngineImpl; class CC_DLL AudioPlayer { + friend class AudioEngineImpl; public: AudioPlayer(); ~AudioPlayer();