Merge pull request #10149 from Dhilan007/v3-audioengine-freeze-fix

Fixed AudioEngine causes game 'freeze' on Android
This commit is contained in:
minggo 2015-01-21 16:23:14 +08:00
commit 6b5b0f9784
2 changed files with 11 additions and 3 deletions

View File

@ -24,6 +24,7 @@
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
#include "AudioEngine-inl.h"
#include <unistd.h>
// for native asset manager
#include <sys/types.h>
#include <android/asset_manager.h>
@ -65,6 +66,7 @@ AudioPlayer::AudioPlayer()
, _duration(0.0f)
, _playOver(false)
, _loop(false)
, _assetFd(0)
{
}
@ -79,6 +81,11 @@ AudioPlayer::~AudioPlayer()
_fdPlayerVolume = nullptr;
_fdPlayerSeek = nullptr;
}
if(_assetFd > 0)
{
close(_assetFd);
_assetFd = 0;
}
}
bool AudioPlayer::init(SLEngineItf engineEngine, SLObjectItf outputMixObject,const std::string& fileFullPath, float volume, bool loop)
@ -110,15 +117,15 @@ bool AudioPlayer::init(SLEngineItf engineEngine, SLObjectItf outputMixObject,con
// open asset as file descriptor
off_t start, length;
int fd = AAsset_openFileDescriptor(asset, &start, &length);
if (fd <= 0){
_assetFd = AAsset_openFileDescriptor(asset, &start, &length);
if (_assetFd <= 0){
AAsset_close(asset);
break;
}
AAsset_close(asset);
// configure audio source
loc_fd = {SL_DATALOCATOR_ANDROIDFD, fd, start, length};
loc_fd = {SL_DATALOCATOR_ANDROIDFD, _assetFd, start, length};
audioSrc.pLocator = &loc_fd;
}

View File

@ -59,6 +59,7 @@ private:
float _duration;
int _audioID;
int _assetFd;
std::function<void (int, const std::string &)> _finishCallback;