2014-09-03 18:19:47 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
|
|
|
|
#include "AudioEngine-inl.h"
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
// for native asset manager
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <android/asset_manager.h>
|
|
|
|
#include <android/asset_manager_jni.h>
|
|
|
|
|
|
|
|
#include "audio/include/AudioEngine.h"
|
|
|
|
#include "base/CCDirector.h"
|
2014-09-10 17:12:46 +08:00
|
|
|
#include "platform/android/CCFileUtils-android.h"
|
2014-09-03 18:19:47 +08:00
|
|
|
|
|
|
|
#include "platform/android/jni/JniHelper.h"
|
|
|
|
#include <android/log.h>
|
|
|
|
#include <jni.h>
|
|
|
|
|
|
|
|
using namespace cocos2d;
|
2014-09-16 10:22:25 +08:00
|
|
|
using namespace cocos2d::experimental;
|
2014-09-03 18:19:47 +08:00
|
|
|
|
|
|
|
void PlayOverEvent(SLPlayItf caller, void* context, SLuint32 playEvent)
|
|
|
|
{
|
|
|
|
if (context && playEvent == SL_PLAYEVENT_HEADATEND)
|
|
|
|
{
|
|
|
|
AudioEngineImpl* engineImpl = (AudioEngineImpl*)context;
|
|
|
|
engineImpl->playerFinishCallback(caller,playEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioPlayer::AudioPlayer()
|
|
|
|
: _fdPlayerObject(nullptr)
|
|
|
|
, _finishCallback(nullptr)
|
|
|
|
, _duration(0.0f)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioPlayer::~AudioPlayer()
|
|
|
|
{
|
|
|
|
if (_fdPlayerObject)
|
|
|
|
{
|
|
|
|
(*_fdPlayerObject)->Destroy(_fdPlayerObject);
|
|
|
|
_fdPlayerObject = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioPlayer::init(SLEngineItf engineEngine, SLObjectItf outputMixObject,const std::string& fileFullPath, float volume, bool loop)
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
SLDataSource audioSrc;
|
|
|
|
|
|
|
|
SLDataLocator_AndroidFD loc_fd;
|
|
|
|
SLDataLocator_URI loc_uri;
|
|
|
|
|
|
|
|
SLDataFormat_MIME format_mime = {SL_DATAFORMAT_MIME, NULL, SL_CONTAINERTYPE_UNSPECIFIED};
|
|
|
|
audioSrc.pFormat = &format_mime;
|
|
|
|
|
|
|
|
if (fileFullPath[0] != '/'){
|
|
|
|
std::string relativePath = "";
|
|
|
|
|
|
|
|
size_t position = fileFullPath.find("assets/");
|
|
|
|
if (0 == position) {
|
|
|
|
// "assets/" is at the beginning of the path and we don't want it
|
|
|
|
relativePath += fileFullPath.substr(strlen("assets/"));
|
|
|
|
} else {
|
|
|
|
relativePath += fileFullPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto asset = AAssetManager_open(cocos2d::FileUtilsAndroid::getAssetManager(), relativePath.c_str(), AASSET_MODE_UNKNOWN);
|
|
|
|
|
|
|
|
// open asset as file descriptor
|
|
|
|
off_t start, length;
|
|
|
|
int fd = AAsset_openFileDescriptor(asset, &start, &length);
|
2014-09-05 11:13:51 +08:00
|
|
|
if (fd <= 0){
|
|
|
|
AAsset_close(asset);
|
|
|
|
break;
|
|
|
|
}
|
2014-09-03 18:19:47 +08:00
|
|
|
AAsset_close(asset);
|
|
|
|
|
|
|
|
// configure audio source
|
|
|
|
loc_fd = {SL_DATALOCATOR_ANDROIDFD, fd, start, length};
|
|
|
|
|
|
|
|
audioSrc.pLocator = &loc_fd;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
loc_uri = {SL_DATALOCATOR_URI , (SLchar*)fileFullPath.c_str()};
|
|
|
|
audioSrc.pLocator = &loc_uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
// configure audio sink
|
|
|
|
SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, outputMixObject};
|
|
|
|
SLDataSink audioSnk = {&loc_outmix, NULL};
|
|
|
|
|
|
|
|
// create audio player
|
|
|
|
const SLInterfaceID ids[3] = {SL_IID_SEEK, SL_IID_PREFETCHSTATUS, SL_IID_VOLUME};
|
|
|
|
const SLboolean req[3] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
|
|
|
|
auto result = (*engineEngine)->CreateAudioPlayer(engineEngine, &_fdPlayerObject, &audioSrc, &audioSnk, 3, ids, req);
|
2014-09-10 17:12:46 +08:00
|
|
|
if(SL_RESULT_SUCCESS != result){ ERRORLOG("create audio player fail"); break; }
|
2014-09-03 18:19:47 +08:00
|
|
|
|
|
|
|
// realize the player
|
|
|
|
result = (*_fdPlayerObject)->Realize(_fdPlayerObject, SL_BOOLEAN_FALSE);
|
2014-09-10 17:12:46 +08:00
|
|
|
if(SL_RESULT_SUCCESS != result){ ERRORLOG("realize the player fail"); break; }
|
2014-09-03 18:19:47 +08:00
|
|
|
|
|
|
|
// get the play interface
|
|
|
|
result = (*_fdPlayerObject)->GetInterface(_fdPlayerObject, SL_IID_PLAY, &_fdPlayerPlay);
|
2014-09-10 17:12:46 +08:00
|
|
|
if(SL_RESULT_SUCCESS != result){ ERRORLOG("get the play interface fail"); break; }
|
2014-09-03 18:19:47 +08:00
|
|
|
|
|
|
|
// get the seek interface
|
|
|
|
result = (*_fdPlayerObject)->GetInterface(_fdPlayerObject, SL_IID_SEEK, &_fdPlayerSeek);
|
2014-09-10 17:12:46 +08:00
|
|
|
if(SL_RESULT_SUCCESS != result){ ERRORLOG("get the seek interface fail"); break; }
|
2014-09-03 18:19:47 +08:00
|
|
|
|
|
|
|
// get the volume interface
|
|
|
|
result = (*_fdPlayerObject)->GetInterface(_fdPlayerObject, SL_IID_VOLUME, &_fdPlayerVolume);
|
2014-09-10 17:12:46 +08:00
|
|
|
if(SL_RESULT_SUCCESS != result){ ERRORLOG("get the volume interface fail"); break; }
|
2014-09-03 18:19:47 +08:00
|
|
|
|
|
|
|
if (loop){
|
|
|
|
(*_fdPlayerSeek)->SetLoop(_fdPlayerSeek, SL_BOOLEAN_TRUE, 0, SL_TIME_UNKNOWN);
|
|
|
|
}
|
|
|
|
|
2014-09-10 13:47:17 +08:00
|
|
|
int dbVolume = 2000 * log10(volume);
|
|
|
|
if(dbVolume < SL_MILLIBEL_MIN){
|
|
|
|
dbVolume = SL_MILLIBEL_MIN;
|
|
|
|
}
|
|
|
|
(*_fdPlayerVolume)->SetVolumeLevel(_fdPlayerVolume, dbVolume);
|
2014-09-03 18:19:47 +08:00
|
|
|
|
|
|
|
result = (*_fdPlayerPlay)->SetPlayState(_fdPlayerPlay, SL_PLAYSTATE_PLAYING);
|
2014-09-10 17:12:46 +08:00
|
|
|
if(SL_RESULT_SUCCESS != result){ ERRORLOG("SetPlayState fail"); break; }
|
2014-09-03 18:19:47 +08:00
|
|
|
|
|
|
|
ret = true;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//====================================================
|
2014-09-05 11:13:51 +08:00
|
|
|
AudioEngineImpl::AudioEngineImpl()
|
2014-09-10 17:12:46 +08:00
|
|
|
: currentAudioID(0)
|
2014-09-03 18:19:47 +08:00
|
|
|
, _engineObject(nullptr)
|
|
|
|
, _engineEngine(nullptr)
|
|
|
|
, _outputMixObject(nullptr)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioEngineImpl::~AudioEngineImpl()
|
|
|
|
{
|
|
|
|
if (_outputMixObject)
|
|
|
|
{
|
|
|
|
(*_outputMixObject)->Destroy(_outputMixObject);
|
|
|
|
}
|
|
|
|
if (_engineObject)
|
|
|
|
{
|
|
|
|
(*_engineObject)->Destroy(_engineObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioEngineImpl::init()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
do{
|
|
|
|
// create engine
|
|
|
|
auto result = slCreateEngine(&_engineObject, 0, nullptr, 0, nullptr, nullptr);
|
2014-09-10 17:12:46 +08:00
|
|
|
if(SL_RESULT_SUCCESS != result){ ERRORLOG("create opensl engine fail"); break; }
|
2014-09-03 18:19:47 +08:00
|
|
|
|
|
|
|
// realize the engine
|
|
|
|
result = (*_engineObject)->Realize(_engineObject, SL_BOOLEAN_FALSE);
|
2014-09-10 17:12:46 +08:00
|
|
|
if(SL_RESULT_SUCCESS != result){ ERRORLOG("realize the engine fail"); break; }
|
2014-09-03 18:19:47 +08:00
|
|
|
|
|
|
|
// get the engine interface, which is needed in order to create other objects
|
|
|
|
result = (*_engineObject)->GetInterface(_engineObject, SL_IID_ENGINE, &_engineEngine);
|
2014-09-10 17:12:46 +08:00
|
|
|
if(SL_RESULT_SUCCESS != result){ ERRORLOG("get the engine interface fail"); break; }
|
2014-09-03 18:19:47 +08:00
|
|
|
|
|
|
|
// create output mix
|
|
|
|
const SLInterfaceID outputMixIIDs[] = {};
|
|
|
|
const SLboolean outputMixReqs[] = {};
|
|
|
|
result = (*_engineEngine)->CreateOutputMix(_engineEngine, &_outputMixObject, 0, outputMixIIDs, outputMixReqs);
|
2014-09-10 17:12:46 +08:00
|
|
|
if(SL_RESULT_SUCCESS != result){ ERRORLOG("create output mix fail"); break; }
|
2014-09-03 18:19:47 +08:00
|
|
|
|
|
|
|
// realize the output mix
|
|
|
|
result = (*_outputMixObject)->Realize(_outputMixObject, SL_BOOLEAN_FALSE);
|
2014-09-10 17:12:46 +08:00
|
|
|
if(SL_RESULT_SUCCESS != result){ ERRORLOG("realize the output mix fail"); break; }
|
2014-09-03 18:19:47 +08:00
|
|
|
|
|
|
|
ret = true;
|
|
|
|
}while (false);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-09-05 11:13:51 +08:00
|
|
|
int AudioEngineImpl::play2d(const std::string &fileFullPath ,bool loop ,float volume)
|
2014-09-03 18:19:47 +08:00
|
|
|
{
|
|
|
|
auto audioId = AudioEngine::INVAILD_AUDIO_ID;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (_engineEngine == nullptr)
|
|
|
|
break;
|
|
|
|
|
2014-09-10 17:12:46 +08:00
|
|
|
auto& player = _audioPlayers[currentAudioID];
|
2014-09-03 18:19:47 +08:00
|
|
|
auto initPlayer = player.init( _engineEngine, _outputMixObject, fileFullPath, volume, loop);
|
|
|
|
if (!initPlayer){
|
2014-09-10 17:12:46 +08:00
|
|
|
_audioPlayers.erase(currentAudioID);
|
2014-09-03 18:19:47 +08:00
|
|
|
log("%s,%d message:create player for %s fail", __func__, __LINE__, fileFullPath.c_str());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-09-10 17:12:46 +08:00
|
|
|
audioId = currentAudioID++;
|
2014-09-03 18:19:47 +08:00
|
|
|
player._audioID = audioId;
|
|
|
|
|
|
|
|
(*(player._fdPlayerPlay))->RegisterCallback(player._fdPlayerPlay, PlayOverEvent, (void*)this);
|
|
|
|
(*(player._fdPlayerPlay))->SetCallbackEventsMask(player._fdPlayerPlay, SL_PLAYEVENT_HEADATEND);
|
|
|
|
|
2014-09-10 13:47:17 +08:00
|
|
|
AudioEngine::_audioIDInfoMap[audioId].state = AudioEngine::AudioState::PLAYING;
|
2014-09-03 18:19:47 +08:00
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return audioId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngineImpl::playerFinishCallback(SLPlayItf caller, SLuint32 playEvent)
|
|
|
|
{
|
|
|
|
auto itend = _audioPlayers.end();
|
|
|
|
for (auto iter = _audioPlayers.begin(); iter != itend; ++iter)
|
|
|
|
{
|
|
|
|
if (iter->second._fdPlayerPlay == caller)
|
|
|
|
{
|
|
|
|
if (iter->second._finishCallback)
|
|
|
|
{
|
2014-09-10 13:47:17 +08:00
|
|
|
iter->second._finishCallback(iter->second._audioID, *AudioEngine::_audioIDInfoMap[iter->second._audioID].filePath);
|
2014-09-03 18:19:47 +08:00
|
|
|
}
|
2014-09-15 17:36:00 +08:00
|
|
|
AudioEngine::remove(iter->second._audioID);
|
|
|
|
_audioPlayers.erase(iter);
|
2014-09-03 18:19:47 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngineImpl::setVolume(int audioID,float volume)
|
|
|
|
{
|
|
|
|
auto& player = _audioPlayers[audioID];
|
2014-09-10 13:47:17 +08:00
|
|
|
int dbVolume = 2000 * log10(volume);
|
|
|
|
if(dbVolume < SL_MILLIBEL_MIN){
|
|
|
|
dbVolume = SL_MILLIBEL_MIN;
|
|
|
|
}
|
|
|
|
auto result = (*player._fdPlayerVolume)->SetVolumeLevel(player._fdPlayerVolume, dbVolume);
|
2014-09-03 18:19:47 +08:00
|
|
|
if(SL_RESULT_SUCCESS != result){
|
2014-09-10 13:47:17 +08:00
|
|
|
log("%s error:%lu",__func__, result);
|
2014-09-03 18:19:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngineImpl::setLoop(int audioID, bool loop)
|
|
|
|
{
|
|
|
|
auto& player = _audioPlayers[audioID];
|
|
|
|
SLboolean loopEnabled = SL_BOOLEAN_TRUE;
|
|
|
|
if (!loop){
|
|
|
|
loopEnabled = SL_BOOLEAN_FALSE;
|
|
|
|
}
|
|
|
|
(*player._fdPlayerSeek)->SetLoop(player._fdPlayerSeek, loopEnabled, 0, SL_TIME_UNKNOWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngineImpl::pause(int audioID)
|
|
|
|
{
|
|
|
|
auto& player = _audioPlayers[audioID];
|
|
|
|
auto result = (*player._fdPlayerPlay)->SetPlayState(player._fdPlayerPlay, SL_PLAYSTATE_PAUSED);
|
|
|
|
if(SL_RESULT_SUCCESS != result){
|
2014-09-10 13:47:17 +08:00
|
|
|
log("%s error:%lu",__func__, result);
|
2014-09-03 18:19:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngineImpl::resume(int audioID)
|
|
|
|
{
|
|
|
|
auto& player = _audioPlayers[audioID];
|
|
|
|
auto result = (*player._fdPlayerPlay)->SetPlayState(player._fdPlayerPlay, SL_PLAYSTATE_PLAYING);
|
|
|
|
if(SL_RESULT_SUCCESS != result){
|
2014-09-10 13:47:17 +08:00
|
|
|
log("%s error:%lu",__func__, result);
|
2014-09-03 18:19:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngineImpl::stop(int audioID)
|
|
|
|
{
|
|
|
|
auto& player = _audioPlayers[audioID];
|
|
|
|
auto result = (*player._fdPlayerPlay)->SetPlayState(player._fdPlayerPlay, SL_PLAYSTATE_STOPPED);
|
|
|
|
if(SL_RESULT_SUCCESS != result){
|
2014-09-10 13:47:17 +08:00
|
|
|
log("%s error:%lu",__func__, result);
|
2014-09-03 18:19:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_audioPlayers.erase(audioID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngineImpl::stopAll()
|
|
|
|
{
|
|
|
|
auto itEnd = _audioPlayers.end();
|
|
|
|
for (auto it = _audioPlayers.begin(); it != itEnd; ++it)
|
|
|
|
{
|
|
|
|
auto result = (*it->second._fdPlayerPlay)->SetPlayState(it->second._fdPlayerPlay, SL_PLAYSTATE_STOPPED);
|
|
|
|
}
|
|
|
|
_audioPlayers.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
float AudioEngineImpl::getDuration(int audioID)
|
|
|
|
{
|
|
|
|
SLmillisecond duration;
|
|
|
|
auto& player = _audioPlayers[audioID];
|
|
|
|
auto result = (*player._fdPlayerPlay)->GetDuration(player._fdPlayerPlay, &duration);
|
|
|
|
if (duration == SL_TIME_UNKNOWN){
|
|
|
|
return AudioEngine::TIME_UNKNOWN;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
player._duration = duration / 1000.0;
|
|
|
|
|
|
|
|
if (player._duration <= 0)
|
|
|
|
{
|
|
|
|
return AudioEngine::TIME_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return player._duration;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float AudioEngineImpl::getCurrentTime(int audioID)
|
|
|
|
{
|
|
|
|
SLmillisecond currPos;
|
|
|
|
auto& player = _audioPlayers[audioID];
|
|
|
|
(*player._fdPlayerPlay)->GetPosition(player._fdPlayerPlay, &currPos);
|
2014-09-10 13:47:17 +08:00
|
|
|
return currPos / 1000.0f;
|
2014-09-03 18:19:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioEngineImpl::setCurrentTime(int audioID, float time)
|
|
|
|
{
|
|
|
|
auto& player = _audioPlayers[audioID];
|
|
|
|
SLmillisecond pos = 1000 * time;
|
|
|
|
auto result = (*player._fdPlayerSeek)->SetPosition(player._fdPlayerSeek, pos, SL_SEEKMODE_ACCURATE);
|
|
|
|
if(SL_RESULT_SUCCESS != result){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngineImpl::setFinishCallback(int audioID, const std::function<void (int, const std::string &)> &callback)
|
|
|
|
{
|
|
|
|
_audioPlayers[audioID]._finishCallback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|