This commit is contained in:
halx99 2019-11-30 06:55:32 +08:00
parent cfeaec9582
commit 9c0ef83757
5 changed files with 40 additions and 8 deletions

View File

@ -241,11 +241,12 @@ bool AudioPlayer::play2d()
void AudioPlayer::rotateBufferThread(int offsetFrame) void AudioPlayer::rotateBufferThread(int offsetFrame)
{ {
char* tmpBuffer = nullptr; char* tmpBuffer = nullptr;
AudioDecoder* decoder = AudioDecoderManager::createDecoder(_audioCache->_fileFullPath.c_str()); auto& fullPath = _audioCache->_fileFullPath;
AudioDecoder* decoder = AudioDecoderManager::createDecoder(fullPath);
long long rotateSleepTime = static_cast<long long>(QUEUEBUFFER_TIME_STEP * 1000) / 2; long long rotateSleepTime = static_cast<long long>(QUEUEBUFFER_TIME_STEP * 1000) / 2;
do do
{ {
BREAK_IF(decoder == nullptr || !decoder->open(_audioCache->_fileFullPath.c_str())); BREAK_IF(decoder == nullptr || !decoder->open(fullPath));
uint32_t framesRead = 0; uint32_t framesRead = 0;
const uint32_t framesToRead = _audioCache->_queBufferFrames; const uint32_t framesToRead = _audioCache->_queBufferFrames;

View File

@ -448,10 +448,10 @@ AUDIO_ID AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float v
void AudioEngineImpl::_play2d(AudioCache *cache, AUDIO_ID audioID) void AudioEngineImpl::_play2d(AudioCache *cache, AUDIO_ID audioID)
{ {
_threadMutex.lock(); //Note: It may bn in sub thread or main thread :(
//Note: It maybe in sub thread or main thread :(
if (!*cache->_isDestroyed && cache->_state == AudioCache::State::READY) if (!*cache->_isDestroyed && cache->_state == AudioCache::State::READY)
{ {
_threadMutex.lock();
auto playerIt = _audioPlayers.find(audioID); auto playerIt = _audioPlayers.find(audioID);
if (playerIt != _audioPlayers.end() && playerIt->second->play2d()) { if (playerIt != _audioPlayers.end() && playerIt->second->play2d()) {
_scheduler->performFunctionInCocosThread([audioID](){ _scheduler->performFunctionInCocosThread([audioID](){
@ -461,6 +461,7 @@ void AudioEngineImpl::_play2d(AudioCache *cache, AUDIO_ID audioID)
} }
}); });
} }
_threadMutex.unlock();
} }
else else
{ {
@ -471,7 +472,6 @@ void AudioEngineImpl::_play2d(AudioCache *cache, AUDIO_ID audioID)
iter->second->_removeByAudioEngine = true; iter->second->_removeByAudioEngine = true;
} }
} }
_threadMutex.unlock();
} }
ALuint AudioEngineImpl::findValidSource() ALuint AudioEngineImpl::findValidSource()
@ -711,7 +711,7 @@ void AudioEngineImpl::update(float dt)
delete player; delete player;
_unusedSourcesPool.push_back(alSource); _unusedSourcesPool.push_back(alSource);
} }
else if (player->_ready && sourceState == AL_STOPPED) { else if (player->_ready && player->isFinished()) {
std::string filePath; std::string filePath;
if (player->_finishCallbak) { if (player->_finishCallbak) {

View File

@ -55,6 +55,8 @@ public:
float getTime() { return _currTime;} float getTime() { return _currTime;}
bool setLoop(bool loop); bool setLoop(bool loop);
bool isFinished() const;
protected: protected:
void setCache(AudioCache* cache); void setCache(AudioCache* cache);
void rotateBufferThread(int offsetFrame); void rotateBufferThread(int offsetFrame);
@ -65,7 +67,7 @@ protected:
float _volume; float _volume;
bool _loop; bool _loop;
std::function<void (int, const std::string &)> _finishCallbak; std::function<void (AUDIO_ID, const std::string &)> _finishCallbak;
bool _isDestroyed; bool _isDestroyed;
bool _removeByAudioEngine; bool _removeByAudioEngine;

View File

@ -327,6 +327,25 @@ void AudioPlayer::rotateBufferThread(int offsetFrame)
alSourceQueueBuffers(_alSource, 1, &bid); alSourceQueueBuffers(_alSource, 1, &bid);
} }
} }
/* Make sure the source hasn't underrun */
else if (sourceState != AL_PAUSED)
{
ALint queued;
/* If no buffers are queued, playback is finished */
alGetSourcei(_alSource, AL_BUFFERS_QUEUED, &queued);
if (queued == 0) {
needToExitThread = true;
}
else {
alSourcePlay(_alSource);
if (alGetError() != AL_NO_ERROR)
{
ALOGE("Error restarting playback!");
needToExitThread = true;
}
}
}
std::unique_lock<std::mutex> lk(_sleepMutex); std::unique_lock<std::mutex> lk(_sleepMutex);
if (_isDestroyed || needToExitThread) { if (_isDestroyed || needToExitThread) {
@ -357,6 +376,16 @@ void AudioPlayer::wakeupRotateThread()
_sleepCondition.notify_all(); _sleepCondition.notify_all();
} }
bool AudioPlayer::isFinished() const
{
if(_streamingSource) return _isRotateThreadExited;
else {
ALint sourceState;
alGetSourcei(_alSource, AL_SOURCE_STATE, &sourceState);
return sourceState == AL_STOPPED;
}
}
bool AudioPlayer::setLoop(bool loop) bool AudioPlayer::setLoop(bool loop)
{ {
if (!_isDestroyed ) { if (!_isDestroyed ) {

View File

@ -78,7 +78,7 @@ protected:
//play by circular buffer //play by circular buffer
float _currTime; float _currTime;
bool _streamingSource; bool _streamingSource;
ALuint _bufferIds[3]; ALuint _bufferIds[QUEUEBUFFER_NUM];
std::thread* _rotateBufferThread; std::thread* _rotateBufferThread;
std::condition_variable _sleepCondition; std::condition_variable _sleepCondition;
std::mutex _sleepMutex; std::mutex _sleepMutex;