Refactoring API:

1.Remove Singleton Pattern
2.Make AudioProfile clear.
This commit is contained in:
Dhilan007 2014-09-05 14:34:30 +08:00
parent 6b4b5ed943
commit 1d900940ab
3 changed files with 13 additions and 19 deletions

View File

@ -133,8 +133,10 @@ int AudioEngine::play2d(const std::string& filePath, bool loop, float volume, co
audioRef.is3dAudio = false;
audioRef.filePath = &it->first;
manage->lastPlayTime = utils::gettime();
manage->audioIDs.push_back(ret);
if (manage) {
manage->lastPlayTime = utils::gettime();
manage->audioIDs.push_back(ret);
}
audioRef.profileManage = manage;
}
} while (0);

View File

@ -33,7 +33,6 @@
#include "AudioPlayer.h"
NS_CC_BEGIN
class AudioEngine;
class AudioProfile;
#define kMaxSources 32
@ -43,11 +42,11 @@ class AudioEngineThreadPool;
class AudioEngineImpl : public cocos2d::Ref
{
public:
AudioEngineImpl(AudioEngine* audioEngine);
AudioEngineImpl();
~AudioEngineImpl();
bool init();
int play2d(const std::string &fileFullPath ,bool loop ,float volume, AudioProfile* profile);
int play2d(const std::string &fileFullPath ,bool loop ,float volume);
void setVolume(int audioID,float volume);
void setLoop(int audioID, bool loop);
bool pause(int audioID);
@ -68,7 +67,6 @@ private:
void _play2d(AudioCache *cache, int audioID);
AudioEngineThreadPool* _threadPool;
AudioEngine* _audioEngine;
ALuint _alSources[kMaxSources];

View File

@ -132,9 +132,8 @@ namespace cocos2d {
};
}
AudioEngineImpl::AudioEngineImpl(AudioEngine* audioEngine)
: _audioEngine(audioEngine)
, _lazyInitLoop(true)
AudioEngineImpl::AudioEngineImpl()
: _lazyInitLoop(true)
, nextAudioID(0)
, _threadPool(nullptr)
{
@ -193,7 +192,7 @@ bool AudioEngineImpl::init()
return ret;
}
int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume, AudioProfile* profile)
int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume)
{
if (s_ALDevice == nullptr) {
return AudioEngine::INVAILD_AUDIO_ID;
@ -233,11 +232,6 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume
_alSourceUsed[alSource] = true;
if (profile) {
profile->lastPlayTime = utils::gettime();
profile->audioIDs.push_back(nextAudioID);
}
if (_lazyInitLoop) {
_lazyInitLoop = false;
@ -254,7 +248,7 @@ void AudioEngineImpl::_play2d(AudioCache *cache, int audioID)
auto playerIt = _audioPlayers.find(audioID);
if (playerIt != _audioPlayers.end()) {
if (playerIt->second.play2d(cache)) {
_audioEngine->_audioInfos[audioID].state = AudioEngine::AudioState::PLAYING;
AudioEngine::_audioInfos[audioID].state = AudioEngine::AudioState::PLAYING;
}
else{
_threadMutex.lock();
@ -455,7 +449,7 @@ void AudioEngineImpl::update(float dt)
if (playerIt != _audioPlayers.end()) {
_alSourceUsed[playerIt->second._alSource] = false;
_audioPlayers.erase(audioID);
_audioEngine->remove(audioID);
AudioEngine::remove(audioID);
}
}
size_t removeCacheCount = _removeCaches.size();
@ -478,12 +472,12 @@ void AudioEngineImpl::update(float dt)
if (player._ready && sourceState == AL_STOPPED) {
_alSourceUsed[player._alSource] = false;
auto& audioInfo = _audioEngine->_audioInfos[audioID];
auto& audioInfo = AudioEngine::_audioInfos[audioID];
if (player._finishCallbak) {
player._finishCallbak(audioID, *audioInfo.filePath);
}
_audioEngine->remove(audioID);
AudioEngine::remove(audioID);
it = _audioPlayers.erase(it);
}