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 uncacheAll();
AudioCache* preload(const std::string& filePath);
void update(float dt);
private:

View File

@ -283,6 +283,24 @@ bool AudioEngineImpl::init()
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)
{
if (s_ALDevice == nullptr) {
@ -303,16 +321,9 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume
return AudioEngine::INVALID_AUDIO_ID;
}
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;
AudioCache* audioCache = preload(filePath);
if (audioCache == nullptr) {
return AudioEngine::INVALID_AUDIO_ID;
}
auto player = &_audioPlayers[_currentAudioID];