AudioEngine:support preload for audio[MAC/iOS]

This commit is contained in:
Wenhai Lin 2015-07-08 18:04:43 +08:00
parent b395ef06d8
commit 5d50663c35
2 changed files with 22 additions and 11 deletions

View File

@ -61,7 +61,7 @@ public:
void uncache(const std::string& filePath); void uncache(const std::string& filePath);
void uncacheAll(); void uncacheAll();
AudioCache* preload(const std::string& filePath);
void update(float dt); void update(float dt);
private: private:

View File

@ -283,6 +283,24 @@ bool AudioEngineImpl::init()
return ret; return ret;
} }
AudioCache* AudioEngineImpl::preload(const std::string& filePath)
{
AudioCache* audioCache = nullptr;
auto it = _audioCaches.find(filePath);
if (it == _audioCaches.end()) {
audioCache = &_audioCaches[filePath];
audioCache->_fileFullPath = FileUtils::getInstance()->fullPathForFilename(filePath);
_threadPool->addTask(std::bind(&AudioCache::readDataTask, audioCache));
}
else {
audioCache = &it->second;
}
return audioCache;
}
int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume) int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume)
{ {
if (s_ALDevice == nullptr) { if (s_ALDevice == nullptr) {
@ -303,16 +321,9 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume
return AudioEngine::INVALID_AUDIO_ID; return AudioEngine::INVALID_AUDIO_ID;
} }
AudioCache* audioCache = nullptr; AudioCache* audioCache = preload(filePath);
auto it = _audioCaches.find(filePath); if (audioCache == nullptr) {
if (it == _audioCaches.end()) { return AudioEngine::INVALID_AUDIO_ID;
audioCache = &_audioCaches[filePath];
audioCache->_fileFullPath = FileUtils::getInstance()->fullPathForFilename(filePath);
_threadPool->addTask(std::bind(&AudioCache::readDataTask, audioCache));
}
else {
audioCache = &it->second;
} }
auto player = &_audioPlayers[_currentAudioID]; auto player = &_audioPlayers[_currentAudioID];