2014-09-03 18:19:21 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2014-09-30 01:07:11 +08:00
|
|
|
|
|
|
|
#include "platform/CCPlatformConfig.h"
|
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
#include "audio/include/AudioEngine.h"
|
2015-07-28 15:27:07 +08:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <queue>
|
2014-09-03 18:19:21 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "base/ccUtils.h"
|
|
|
|
|
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "audio/android/AudioEngine-inl.h"
|
2014-09-29 10:15:41 +08:00
|
|
|
#elif CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "audio/apple/AudioEngine-inl.h"
|
2014-09-29 10:15:41 +08:00
|
|
|
#elif CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "audio/win32/AudioEngine-win32.h"
|
2015-05-16 09:17:52 +08:00
|
|
|
#elif CC_TARGET_PLATFORM == CC_PLATFORM_WINRT
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "audio/winrt/AudioEngine-winrt.h"
|
2015-11-25 09:54:12 +08:00
|
|
|
#elif CC_TARGET_PLATFORM == CC_PLATFORM_LINUX
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "audio/linux/AudioEngine-linux.h"
|
2016-04-28 09:49:55 +08:00
|
|
|
#elif CC_TARGET_PLATFORM == CC_PLATFORM_TIZEN
|
|
|
|
#include "audio/tizen/AudioEngine-tizen.h"
|
2014-09-03 18:19:21 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define TIME_DELAY_PRECISION 0.0001
|
|
|
|
|
2014-11-13 22:02:28 +08:00
|
|
|
#ifdef ERROR
|
|
|
|
#undef ERROR
|
|
|
|
#endif // ERROR
|
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
using namespace cocos2d;
|
2014-09-16 10:22:25 +08:00
|
|
|
using namespace cocos2d::experimental;
|
2014-09-03 18:19:21 +08:00
|
|
|
|
2015-01-01 08:51:45 +08:00
|
|
|
const int AudioEngine::INVALID_AUDIO_ID = -1;
|
2014-09-03 18:19:21 +08:00
|
|
|
const float AudioEngine::TIME_UNKNOWN = -1.0f;
|
|
|
|
|
2014-09-05 11:13:51 +08:00
|
|
|
//audio file path,audio IDs
|
2014-09-10 13:47:17 +08:00
|
|
|
std::unordered_map<std::string,std::list<int>> AudioEngine::_audioPathIDMap;
|
2014-09-10 14:09:42 +08:00
|
|
|
//profileName,ProfileHelper
|
|
|
|
std::unordered_map<std::string, AudioEngine::ProfileHelper> AudioEngine::_audioPathProfileHelperMap;
|
2015-01-23 09:02:33 +08:00
|
|
|
unsigned int AudioEngine::_maxInstances = MAX_AUDIOINSTANCES;
|
2014-09-29 10:15:41 +08:00
|
|
|
AudioEngine::ProfileHelper* AudioEngine::_defaultProfileHelper = nullptr;
|
2014-09-10 13:47:17 +08:00
|
|
|
std::unordered_map<int, AudioEngine::AudioInfo> AudioEngine::_audioIDInfoMap;
|
2014-09-05 11:13:51 +08:00
|
|
|
AudioEngineImpl* AudioEngine::_audioEngineImpl = nullptr;
|
2014-09-03 18:19:21 +08:00
|
|
|
|
2015-07-09 14:31:03 +08:00
|
|
|
AudioEngine::AudioEngineThreadPool* AudioEngine::s_threadPool = nullptr;
|
|
|
|
|
|
|
|
class AudioEngine::AudioEngineThreadPool
|
|
|
|
{
|
|
|
|
public:
|
2016-12-29 10:05:13 +08:00
|
|
|
AudioEngineThreadPool(int threads = 4)
|
|
|
|
: _stop(false)
|
2015-07-09 14:31:03 +08:00
|
|
|
{
|
2015-07-28 15:27:07 +08:00
|
|
|
for (int index = 0; index < threads; ++index)
|
|
|
|
{
|
|
|
|
_workers.emplace_back(std::thread(std::bind(&AudioEngineThreadPool::threadFunc, this)));
|
2015-07-09 14:31:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void addTask(const std::function<void()> &task){
|
2015-07-28 15:27:07 +08:00
|
|
|
std::unique_lock<std::mutex> lk(_queueMutex);
|
|
|
|
_taskQueue.emplace(task);
|
|
|
|
_taskCondition.notify_one();
|
2015-07-09 14:31:03 +08:00
|
|
|
}
|
|
|
|
|
2015-07-28 15:27:07 +08:00
|
|
|
~AudioEngineThreadPool()
|
2015-07-09 14:31:03 +08:00
|
|
|
{
|
2015-07-28 15:27:07 +08:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(_queueMutex);
|
|
|
|
_stop = true;
|
|
|
|
_taskCondition.notify_all();
|
|
|
|
}
|
2015-07-09 14:31:03 +08:00
|
|
|
|
2016-12-29 10:05:13 +08:00
|
|
|
for (auto&& worker : _workers) {
|
|
|
|
worker.join();
|
2015-07-09 14:31:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-07-28 15:27:07 +08:00
|
|
|
void threadFunc()
|
2015-07-09 14:31:03 +08:00
|
|
|
{
|
2015-07-28 15:27:07 +08:00
|
|
|
while (true) {
|
2015-07-09 14:31:03 +08:00
|
|
|
std::function<void()> task = nullptr;
|
|
|
|
{
|
2015-07-28 15:27:07 +08:00
|
|
|
std::unique_lock<std::mutex> lk(_queueMutex);
|
|
|
|
if (_stop)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!_taskQueue.empty())
|
|
|
|
{
|
|
|
|
task = std::move(_taskQueue.front());
|
|
|
|
_taskQueue.pop();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_taskCondition.wait(lk);
|
|
|
|
continue;
|
|
|
|
}
|
2015-07-09 14:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
task();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 15:27:07 +08:00
|
|
|
std::vector<std::thread> _workers;
|
|
|
|
std::queue< std::function<void()> > _taskQueue;
|
2015-07-09 14:31:03 +08:00
|
|
|
|
2015-07-28 15:27:07 +08:00
|
|
|
std::mutex _queueMutex;
|
|
|
|
std::condition_variable _taskCondition;
|
|
|
|
bool _stop;
|
2015-07-09 14:31:03 +08:00
|
|
|
};
|
|
|
|
|
2014-09-05 11:13:51 +08:00
|
|
|
void AudioEngine::end()
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
2015-07-09 14:31:03 +08:00
|
|
|
if (s_threadPool)
|
|
|
|
{
|
|
|
|
delete s_threadPool;
|
|
|
|
s_threadPool = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-09-05 11:13:51 +08:00
|
|
|
delete _audioEngineImpl;
|
|
|
|
_audioEngineImpl = nullptr;
|
2014-09-03 18:19:21 +08:00
|
|
|
|
2014-09-10 14:09:42 +08:00
|
|
|
delete _defaultProfileHelper;
|
|
|
|
_defaultProfileHelper = nullptr;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
2014-09-05 11:13:51 +08:00
|
|
|
bool AudioEngine::lazyInit()
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
2014-09-05 11:13:51 +08:00
|
|
|
if (_audioEngineImpl == nullptr)
|
|
|
|
{
|
|
|
|
_audioEngineImpl = new (std::nothrow) AudioEngineImpl();
|
|
|
|
if(!_audioEngineImpl || !_audioEngineImpl->init() ){
|
2014-09-29 10:15:41 +08:00
|
|
|
delete _audioEngineImpl;
|
|
|
|
_audioEngineImpl = nullptr;
|
2014-09-05 11:13:51 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
2016-12-29 10:22:45 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
|
2015-07-09 14:31:03 +08:00
|
|
|
if (_audioEngineImpl && s_threadPool == nullptr)
|
|
|
|
{
|
2016-12-29 10:05:13 +08:00
|
|
|
s_threadPool = new (std::nothrow) AudioEngineThreadPool();
|
2015-07-09 14:31:03 +08:00
|
|
|
}
|
2016-12-29 10:22:45 +08:00
|
|
|
#endif
|
2015-07-09 14:31:03 +08:00
|
|
|
|
2014-09-05 11:13:51 +08:00
|
|
|
return true;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
2014-09-05 11:13:51 +08:00
|
|
|
int AudioEngine::play2d(const std::string& filePath, bool loop, float volume, const AudioProfile *profile)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
2015-01-01 08:51:45 +08:00
|
|
|
int ret = AudioEngine::INVALID_AUDIO_ID;
|
2014-09-03 18:19:21 +08:00
|
|
|
|
|
|
|
do {
|
2014-09-10 13:47:17 +08:00
|
|
|
if ( !lazyInit() ){
|
|
|
|
break;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
2014-09-05 11:13:51 +08:00
|
|
|
if ( !FileUtils::getInstance()->isFileExist(filePath)){
|
2014-09-03 18:19:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-09-29 10:15:41 +08:00
|
|
|
auto profileHelper = _defaultProfileHelper;
|
|
|
|
if (profile && profile != &profileHelper->profile){
|
2014-09-05 11:13:51 +08:00
|
|
|
CC_ASSERT(!profile->name.empty());
|
2014-09-29 10:15:41 +08:00
|
|
|
profileHelper = &_audioPathProfileHelperMap[profile->name];
|
|
|
|
profileHelper->profile = *profile;
|
2014-09-05 11:13:51 +08:00
|
|
|
}
|
|
|
|
|
2014-09-10 13:47:17 +08:00
|
|
|
if (_audioIDInfoMap.size() >= _maxInstances) {
|
2014-09-05 11:13:51 +08:00
|
|
|
log("Fail to play %s cause by limited max instance of AudioEngine",filePath.c_str());
|
|
|
|
break;
|
|
|
|
}
|
2014-09-29 10:15:41 +08:00
|
|
|
if (profileHelper)
|
2014-09-05 11:13:51 +08:00
|
|
|
{
|
2014-09-29 10:15:41 +08:00
|
|
|
if(profileHelper->profile.maxInstances != 0 && profileHelper->audioIDs.size() >= profileHelper->profile.maxInstances){
|
2014-09-05 11:13:51 +08:00
|
|
|
log("Fail to play %s cause by limited max instance of AudioProfile",filePath.c_str());
|
|
|
|
break;
|
|
|
|
}
|
2014-09-29 10:15:41 +08:00
|
|
|
if (profileHelper->profile.minDelay > TIME_DELAY_PRECISION) {
|
2014-09-05 11:13:51 +08:00
|
|
|
auto currTime = utils::gettime();
|
2014-09-29 10:15:41 +08:00
|
|
|
if (profileHelper->lastPlayTime > TIME_DELAY_PRECISION && currTime - profileHelper->lastPlayTime <= profileHelper->profile.minDelay) {
|
2014-09-05 11:13:51 +08:00
|
|
|
log("Fail to play %s cause by limited minimum delay",filePath.c_str());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (volume < 0.0f) {
|
|
|
|
volume = 0.0f;
|
|
|
|
}
|
|
|
|
else if (volume > 1.0f){
|
|
|
|
volume = 1.0f;
|
|
|
|
}
|
|
|
|
|
2014-09-05 11:13:51 +08:00
|
|
|
ret = _audioEngineImpl->play2d(filePath, loop, volume);
|
2015-01-01 08:51:45 +08:00
|
|
|
if (ret != INVALID_AUDIO_ID)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
2014-09-10 13:47:17 +08:00
|
|
|
_audioPathIDMap[filePath].push_back(ret);
|
|
|
|
auto it = _audioPathIDMap.find(filePath);
|
2014-09-03 18:19:21 +08:00
|
|
|
|
2014-09-10 13:47:17 +08:00
|
|
|
auto& audioRef = _audioIDInfoMap[ret];
|
2014-09-03 18:19:21 +08:00
|
|
|
audioRef.volume = volume;
|
|
|
|
audioRef.loop = loop;
|
|
|
|
audioRef.filePath = &it->first;
|
2014-09-05 11:13:51 +08:00
|
|
|
|
2014-09-29 10:15:41 +08:00
|
|
|
if (profileHelper) {
|
|
|
|
profileHelper->lastPlayTime = utils::gettime();
|
|
|
|
profileHelper->audioIDs.push_back(ret);
|
2014-09-05 14:34:30 +08:00
|
|
|
}
|
2014-09-29 10:15:41 +08:00
|
|
|
audioRef.profileHelper = profileHelper;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngine::setLoop(int audioID, bool loop)
|
|
|
|
{
|
2014-09-10 13:47:17 +08:00
|
|
|
auto it = _audioIDInfoMap.find(audioID);
|
|
|
|
if (it != _audioIDInfoMap.end() && it->second.loop != loop){
|
2014-09-03 18:19:21 +08:00
|
|
|
_audioEngineImpl->setLoop(audioID, loop);
|
|
|
|
it->second.loop = loop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngine::setVolume(int audioID, float volume)
|
|
|
|
{
|
2014-09-10 13:47:17 +08:00
|
|
|
auto it = _audioIDInfoMap.find(audioID);
|
2014-09-10 14:09:42 +08:00
|
|
|
if (it != _audioIDInfoMap.end()){
|
2014-09-03 18:19:21 +08:00
|
|
|
if (volume < 0.0f) {
|
|
|
|
volume = 0.0f;
|
|
|
|
}
|
|
|
|
else if (volume > 1.0f){
|
|
|
|
volume = 1.0f;
|
|
|
|
}
|
2014-09-10 14:09:42 +08:00
|
|
|
|
|
|
|
if (it->second.volume != volume){
|
|
|
|
_audioEngineImpl->setVolume(audioID, volume);
|
|
|
|
it->second.volume = volume;
|
|
|
|
}
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngine::pause(int audioID)
|
|
|
|
{
|
2014-09-10 13:47:17 +08:00
|
|
|
auto it = _audioIDInfoMap.find(audioID);
|
|
|
|
if (it != _audioIDInfoMap.end() && it->second.state == AudioState::PLAYING){
|
2014-09-03 18:19:21 +08:00
|
|
|
_audioEngineImpl->pause(audioID);
|
|
|
|
it->second.state = AudioState::PAUSED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngine::pauseAll()
|
|
|
|
{
|
2014-09-10 13:47:17 +08:00
|
|
|
auto itEnd = _audioIDInfoMap.end();
|
|
|
|
for (auto it = _audioIDInfoMap.begin(); it != itEnd; ++it)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
|
|
|
if (it->second.state == AudioState::PLAYING)
|
|
|
|
{
|
|
|
|
_audioEngineImpl->pause(it->first);
|
|
|
|
it->second.state = AudioState::PAUSED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngine::resume(int audioID)
|
|
|
|
{
|
2014-09-10 13:47:17 +08:00
|
|
|
auto it = _audioIDInfoMap.find(audioID);
|
|
|
|
if (it != _audioIDInfoMap.end() && it->second.state == AudioState::PAUSED){
|
2014-09-03 18:19:21 +08:00
|
|
|
_audioEngineImpl->resume(audioID);
|
|
|
|
it->second.state = AudioState::PLAYING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngine::resumeAll()
|
|
|
|
{
|
2014-09-10 13:47:17 +08:00
|
|
|
auto itEnd = _audioIDInfoMap.end();
|
|
|
|
for (auto it = _audioIDInfoMap.begin(); it != itEnd; ++it)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
|
|
|
if (it->second.state == AudioState::PAUSED)
|
|
|
|
{
|
|
|
|
_audioEngineImpl->resume(it->first);
|
|
|
|
it->second.state = AudioState::PLAYING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngine::stop(int audioID)
|
|
|
|
{
|
2014-09-10 13:47:17 +08:00
|
|
|
auto it = _audioIDInfoMap.find(audioID);
|
|
|
|
if (it != _audioIDInfoMap.end()){
|
2014-09-03 18:19:21 +08:00
|
|
|
_audioEngineImpl->stop(audioID);
|
|
|
|
|
2014-09-10 13:47:17 +08:00
|
|
|
remove(audioID);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngine::remove(int audioID)
|
|
|
|
{
|
2014-09-10 13:47:17 +08:00
|
|
|
auto it = _audioIDInfoMap.find(audioID);
|
|
|
|
if (it != _audioIDInfoMap.end()){
|
2014-09-10 14:09:42 +08:00
|
|
|
if (it->second.profileHelper) {
|
|
|
|
it->second.profileHelper->audioIDs.remove(audioID);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
2014-09-10 13:47:17 +08:00
|
|
|
_audioPathIDMap[*it->second.filePath].remove(audioID);
|
|
|
|
_audioIDInfoMap.erase(audioID);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngine::stopAll()
|
|
|
|
{
|
2014-09-14 22:48:41 +08:00
|
|
|
if(!_audioEngineImpl){
|
|
|
|
return;
|
|
|
|
}
|
2014-09-03 18:19:21 +08:00
|
|
|
_audioEngineImpl->stopAll();
|
2014-09-10 13:47:17 +08:00
|
|
|
auto itEnd = _audioIDInfoMap.end();
|
|
|
|
for (auto it = _audioIDInfoMap.begin(); it != itEnd; ++it)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
2014-09-10 14:09:42 +08:00
|
|
|
if (it->second.profileHelper){
|
|
|
|
it->second.profileHelper->audioIDs.remove(it->first);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
}
|
2014-09-10 13:47:17 +08:00
|
|
|
_audioPathIDMap.clear();
|
|
|
|
_audioIDInfoMap.clear();
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngine::uncache(const std::string &filePath)
|
|
|
|
{
|
2016-08-11 16:06:59 +08:00
|
|
|
auto audioIDsIter = _audioPathIDMap.find(filePath);
|
|
|
|
if (audioIDsIter != _audioPathIDMap.end())
|
|
|
|
{
|
|
|
|
//@Note: For safely iterating elements from the audioID list, we need to copy the list
|
|
|
|
// since 'AudioEngine::remove' may be invoked in '_audioEngineImpl->stop' synchronously.
|
|
|
|
// If this happens, it will break the iteration, and crash will appear on some devices.
|
|
|
|
std::list<int> copiedIDs(audioIDsIter->second);
|
|
|
|
|
|
|
|
for (int audioID : copiedIDs)
|
|
|
|
{
|
2014-09-03 18:19:21 +08:00
|
|
|
_audioEngineImpl->stop(audioID);
|
|
|
|
|
2014-09-10 13:47:17 +08:00
|
|
|
auto itInfo = _audioIDInfoMap.find(audioID);
|
2016-08-11 16:06:59 +08:00
|
|
|
if (itInfo != _audioIDInfoMap.end())
|
|
|
|
{
|
|
|
|
if (itInfo->second.profileHelper)
|
|
|
|
{
|
2014-09-10 14:09:42 +08:00
|
|
|
itInfo->second.profileHelper->audioIDs.remove(audioID);
|
2014-09-10 13:47:17 +08:00
|
|
|
}
|
|
|
|
_audioIDInfoMap.erase(audioID);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
}
|
2014-09-10 13:47:17 +08:00
|
|
|
_audioPathIDMap.erase(filePath);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
2014-09-29 10:15:41 +08:00
|
|
|
|
2016-08-11 16:06:59 +08:00
|
|
|
if (_audioEngineImpl)
|
|
|
|
{
|
2014-09-29 10:15:41 +08:00
|
|
|
_audioEngineImpl->uncache(filePath);
|
|
|
|
}
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngine::uncacheAll()
|
|
|
|
{
|
2014-09-14 22:48:41 +08:00
|
|
|
if(!_audioEngineImpl){
|
|
|
|
return;
|
|
|
|
}
|
2014-09-03 18:19:21 +08:00
|
|
|
stopAll();
|
|
|
|
_audioEngineImpl->uncacheAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
float AudioEngine::getDuration(int audioID)
|
|
|
|
{
|
2014-09-10 13:47:17 +08:00
|
|
|
auto it = _audioIDInfoMap.find(audioID);
|
2017-02-04 10:34:34 +08:00
|
|
|
if (it != _audioIDInfoMap.end() && it->second.state != AudioState::INITIALIZING)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
|
|
|
if (it->second.duration == TIME_UNKNOWN)
|
|
|
|
{
|
|
|
|
it->second.duration = _audioEngineImpl->getDuration(audioID);
|
|
|
|
}
|
|
|
|
return it->second.duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TIME_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioEngine::setCurrentTime(int audioID, float time)
|
|
|
|
{
|
2014-09-10 13:47:17 +08:00
|
|
|
auto it = _audioIDInfoMap.find(audioID);
|
2017-02-04 10:34:34 +08:00
|
|
|
if (it != _audioIDInfoMap.end() && it->second.state != AudioState::INITIALIZING) {
|
2014-09-03 18:19:21 +08:00
|
|
|
return _audioEngineImpl->setCurrentTime(audioID, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float AudioEngine::getCurrentTime(int audioID)
|
|
|
|
{
|
2014-09-10 13:47:17 +08:00
|
|
|
auto it = _audioIDInfoMap.find(audioID);
|
2017-02-04 10:34:34 +08:00
|
|
|
if (it != _audioIDInfoMap.end() && it->second.state != AudioState::INITIALIZING) {
|
2014-09-03 18:19:21 +08:00
|
|
|
return _audioEngineImpl->getCurrentTime(audioID);
|
|
|
|
}
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngine::setFinishCallback(int audioID, const std::function<void (int, const std::string &)> &callback)
|
|
|
|
{
|
2014-09-10 13:47:17 +08:00
|
|
|
auto it = _audioIDInfoMap.find(audioID);
|
|
|
|
if (it != _audioIDInfoMap.end()){
|
2014-09-03 18:19:21 +08:00
|
|
|
_audioEngineImpl->setFinishCallback(audioID, callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 13:47:17 +08:00
|
|
|
bool AudioEngine::setMaxAudioInstance(int maxInstances)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
2014-09-10 17:12:46 +08:00
|
|
|
if (maxInstances > 0 && maxInstances <= MAX_AUDIOINSTANCES) {
|
2014-09-03 18:19:21 +08:00
|
|
|
_maxInstances = maxInstances;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-05 11:13:51 +08:00
|
|
|
bool AudioEngine::isLoop(int audioID)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
2014-09-10 13:47:17 +08:00
|
|
|
auto tmpIterator = _audioIDInfoMap.find(audioID);
|
|
|
|
if (tmpIterator != _audioIDInfoMap.end())
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
|
|
|
return tmpIterator->second.loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
log("AudioEngine::isLoop-->The audio instance %d is non-existent", audioID);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-05 11:13:51 +08:00
|
|
|
float AudioEngine::getVolume(int audioID)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
2014-09-10 13:47:17 +08:00
|
|
|
auto tmpIterator = _audioIDInfoMap.find(audioID);
|
|
|
|
if (tmpIterator != _audioIDInfoMap.end())
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
|
|
|
return tmpIterator->second.volume;
|
|
|
|
}
|
|
|
|
|
|
|
|
log("AudioEngine::getVolume-->The audio instance %d is non-existent", audioID);
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
2014-09-05 11:13:51 +08:00
|
|
|
AudioEngine::AudioState AudioEngine::getState(int audioID)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
2014-09-10 13:47:17 +08:00
|
|
|
auto tmpIterator = _audioIDInfoMap.find(audioID);
|
|
|
|
if (tmpIterator != _audioIDInfoMap.end())
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
|
|
|
return tmpIterator->second.state;
|
|
|
|
}
|
2014-09-10 13:47:17 +08:00
|
|
|
|
2014-09-09 11:41:17 +08:00
|
|
|
return AudioState::ERROR;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioProfile* AudioEngine::getProfile(int audioID)
|
|
|
|
{
|
2014-09-10 13:47:17 +08:00
|
|
|
auto it = _audioIDInfoMap.find(audioID);
|
|
|
|
if (it != _audioIDInfoMap.end())
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
2014-09-10 14:09:42 +08:00
|
|
|
return &it->second.profileHelper->profile;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-09-05 11:13:51 +08:00
|
|
|
AudioProfile* AudioEngine::getDefaultProfile()
|
|
|
|
{
|
2014-09-10 14:09:42 +08:00
|
|
|
if (_defaultProfileHelper == nullptr)
|
2014-09-05 11:13:51 +08:00
|
|
|
{
|
2014-09-10 14:09:42 +08:00
|
|
|
_defaultProfileHelper = new (std::nothrow) ProfileHelper();
|
2014-09-05 11:13:51 +08:00
|
|
|
}
|
|
|
|
|
2014-09-10 14:09:42 +08:00
|
|
|
return &_defaultProfileHelper->profile;
|
2014-09-05 11:13:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioProfile* AudioEngine::getProfile(const std::string &name)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
2014-09-10 14:09:42 +08:00
|
|
|
auto it = _audioPathProfileHelperMap.find(name);
|
|
|
|
if (it != _audioPathProfileHelperMap.end()) {
|
2014-09-05 11:13:51 +08:00
|
|
|
return &it->second.profile;
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
2015-07-28 15:27:07 +08:00
|
|
|
void AudioEngine::preload(const std::string& filePath, std::function<void(bool isSuccess)> callback)
|
2015-07-08 10:48:20 +08:00
|
|
|
{
|
|
|
|
lazyInit();
|
|
|
|
|
|
|
|
if (_audioEngineImpl)
|
|
|
|
{
|
|
|
|
if (!FileUtils::getInstance()->isFileExist(filePath)){
|
2015-07-28 15:27:07 +08:00
|
|
|
if (callback)
|
|
|
|
{
|
|
|
|
callback(false);
|
|
|
|
}
|
2015-07-08 10:48:20 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-28 15:27:07 +08:00
|
|
|
_audioEngineImpl->preload(filePath, callback);
|
2015-07-08 10:48:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 15:27:07 +08:00
|
|
|
void AudioEngine::addTask(const std::function<void()>& task)
|
2015-07-09 14:31:03 +08:00
|
|
|
{
|
|
|
|
lazyInit();
|
|
|
|
|
|
|
|
if (_audioEngineImpl && s_threadPool)
|
|
|
|
{
|
|
|
|
s_threadPool->addTask(task);
|
|
|
|
}
|
|
|
|
}
|