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.is3dAudio = false;
audioRef.filePath = &it->first; audioRef.filePath = &it->first;
manage->lastPlayTime = utils::gettime(); if (manage) {
manage->audioIDs.push_back(ret); manage->lastPlayTime = utils::gettime();
manage->audioIDs.push_back(ret);
}
audioRef.profileManage = manage; audioRef.profileManage = manage;
} }
} while (0); } while (0);

View File

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

View File

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