From ac59713ed0e7db23f1941460b82dc66abd2c7651 Mon Sep 17 00:00:00 2001 From: xiaoyifang Date: Mon, 13 Feb 2023 09:23:13 +0800 Subject: [PATCH] fix: repeat play the first played sound --- multimediaaudioplayer.cc | 20 ++++++++++++-------- multimediaaudioplayer.hh | 3 ++- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/multimediaaudioplayer.cc b/multimediaaudioplayer.cc index 1d482ce9..466996cf 100644 --- a/multimediaaudioplayer.cc +++ b/multimediaaudioplayer.cc @@ -29,14 +29,14 @@ MultimediaAudioPlayer::MultimediaAudioPlayer() QString MultimediaAudioPlayer::play( const char * data, int size ) { stop(); - - audioBuffer.setData( data, size ); - if( !audioBuffer.open( QIODevice::ReadOnly ) ) + audioBuffer = new QBuffer(); + audioBuffer->setData( data, size ); + if( !audioBuffer->open( QIODevice::ReadOnly ) ) return tr( "Couldn't open audio buffer for reading." ); -#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) - player.setSourceDevice (&audioBuffer ); +#if( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) ) + player.setSourceDevice( audioBuffer ); #else - player.setMedia( QMediaContent(), &audioBuffer ); + player.setMedia( QMediaContent(), audioBuffer ); #endif player.play(); return QString(); @@ -48,8 +48,12 @@ void MultimediaAudioPlayer::stop() #if (QT_VERSION < QT_VERSION_CHECK(6,0,0)) player.setMedia( QMediaContent() ); // Forget about audioBuffer. #endif - audioBuffer.close(); - audioBuffer.setData( QByteArray() ); // Free memory. + if( audioBuffer ) + { + audioBuffer->close(); + audioBuffer->setData( QByteArray() ); // Free memory. + audioBuffer.clear(); + } } void MultimediaAudioPlayer::onMediaPlayerError() diff --git a/multimediaaudioplayer.hh b/multimediaaudioplayer.hh index 472fe3c1..2a9e5ea5 100644 --- a/multimediaaudioplayer.hh +++ b/multimediaaudioplayer.hh @@ -12,6 +12,7 @@ #if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) #include #endif +#include class MultimediaAudioPlayer : public AudioPlayerInterface { @@ -26,7 +27,7 @@ private slots: void onMediaPlayerError(); private: - QBuffer audioBuffer; + QPointer audioBuffer; QMediaPlayer player; ///< Depends on audioBuffer. #if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) QAudioOutput audioOutput;