diff --git a/cocos/audio/winrt/AudioCachePlayer.cpp b/cocos/audio/winrt/AudioCachePlayer.cpp index d6dbbd3af5..9f2cdb2f5b 100644 --- a/cocos/audio/winrt/AudioCachePlayer.cpp +++ b/cocos/audio/winrt/AudioCachePlayer.cpp @@ -332,6 +332,9 @@ bool AudioPlayer::play2d(AudioCache* cache) _duration = getDuration(); ret = _play(); } + else { + error(); + } } return ret; @@ -342,6 +345,7 @@ void AudioPlayer::init() do { memset(&_xaBuffer, 0, sizeof(_xaBuffer)); if (FAILED(XAudio2Create(_xaEngine.ReleaseAndGetAddressOf()))) { + error(); break; } @@ -354,8 +358,10 @@ void AudioPlayer::init() _xaEngine->RegisterForCallbacks(this); if (FAILED(_xaEngine->CreateMasteringVoice(&_xaMasterVoice, XAUDIO2_DEFAULT_CHANNELS, XAUDIO2_DEFAULT_SAMPLERATE, 0, nullptr, nullptr, AudioCategory_GameMedia))) { + error(); break; } + _ready = true; _state = AudioPlayerState::READY; } while (false); @@ -363,10 +369,12 @@ void AudioPlayer::init() void AudioPlayer::free() { + _ready = false; _stop(); memset(&_xaBuffer, 0, sizeof(_xaBuffer)); if (_xaEngine) { + _xaEngine->UnregisterForCallbacks(this); _xaEngine->StopEngine(); } @@ -430,6 +438,7 @@ void AudioPlayer::error() _criticalError = true; _ready = false; _state = AudioPlayerState::ERRORED; + CCLOG("Audio system encountered error."); } void AudioPlayer::popBuffer() @@ -537,13 +546,15 @@ void AudioPlayer::OnProcessingPassEnd() void AudioPlayer::OnCriticalError(HRESULT err) { UNREFERENCED_PARAMETER(err); - error(); + if (_ready) { + error(); + } } // IXAudio2VoiceCallback void AudioPlayer::OnVoiceProcessingPassStart(UINT32 uBytesRequired) { - if (uBytesRequired && _isStreaming){ + if (_ready && uBytesRequired && _isStreaming){ submitBuffers(); } } @@ -554,7 +565,9 @@ void AudioPlayer::OnVoiceProcessingPassEnd() void AudioPlayer::OnStreamEnd() { - onBufferRunOut(); + if (_ready) { + onBufferRunOut(); + } } void AudioPlayer::OnBufferStart(void* pBufferContext) @@ -565,14 +578,16 @@ void AudioPlayer::OnBufferStart(void* pBufferContext) void AudioPlayer::OnBufferEnd(void* pBufferContext) { UNREFERENCED_PARAMETER(pBufferContext); - updateState(); + if (_ready) { + updateState(); + } } void AudioPlayer::OnLoopEnd(void* pBufferContext) { UNREFERENCED_PARAMETER(pBufferContext); - if (!_loop) { + if (_ready && !_loop) { _stop(); } } @@ -581,7 +596,9 @@ void AudioPlayer::OnVoiceError(void* pBufferContext, HRESULT err) { UNREFERENCED_PARAMETER(pBufferContext); UNREFERENCED_PARAMETER(err); - error(); + if (_ready) { + error(); + } } #endif