From 5fd9ccfecba550687121e23a0e92ad5dd5fe9664 Mon Sep 17 00:00:00 2001 From: WenhaiLin Date: Wed, 21 Jan 2015 16:18:47 +0800 Subject: [PATCH] Fixed AudioEngine causes game 'freeze' on Android --- cocos/audio/android/AudioEngine-inl.cpp | 13 ++++++++++--- cocos/audio/android/AudioEngine-inl.h | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cocos/audio/android/AudioEngine-inl.cpp b/cocos/audio/android/AudioEngine-inl.cpp index 3c7d3cd1fc..53efb94ea6 100644 --- a/cocos/audio/android/AudioEngine-inl.cpp +++ b/cocos/audio/android/AudioEngine-inl.cpp @@ -24,6 +24,7 @@ #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID #include "AudioEngine-inl.h" +#include // for native asset manager #include #include @@ -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; } diff --git a/cocos/audio/android/AudioEngine-inl.h b/cocos/audio/android/AudioEngine-inl.h index c28e0edb53..8f42a6c00d 100644 --- a/cocos/audio/android/AudioEngine-inl.h +++ b/cocos/audio/android/AudioEngine-inl.h @@ -59,6 +59,7 @@ private: float _duration; int _audioID; + int _assetFd; std::function _finishCallback;