Merge pull request #346 from xiaoyifang/fix/sound-repeat

fix: always repeat the first played sound
This commit is contained in:
xiaoyifang 2023-02-13 11:40:10 +08:00 committed by GitHub
commit b5cc1a9fdd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 9 deletions

View file

@ -29,14 +29,14 @@ MultimediaAudioPlayer::MultimediaAudioPlayer()
QString MultimediaAudioPlayer::play( const char * data, int size ) QString MultimediaAudioPlayer::play( const char * data, int size )
{ {
stop(); stop();
audioBuffer = new QBuffer();
audioBuffer.setData( data, size ); audioBuffer->setData( data, size );
if( !audioBuffer.open( QIODevice::ReadOnly ) ) if( !audioBuffer->open( QIODevice::ReadOnly ) )
return tr( "Couldn't open audio buffer for reading." ); return tr( "Couldn't open audio buffer for reading." );
#if( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) ) #if( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) )
player.setSourceDevice (&audioBuffer ); player.setSourceDevice( audioBuffer );
#else #else
player.setMedia( QMediaContent(), &audioBuffer ); player.setMedia( QMediaContent(), audioBuffer );
#endif #endif
player.play(); player.play();
return QString(); return QString();
@ -48,8 +48,12 @@ void MultimediaAudioPlayer::stop()
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0)) #if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
player.setMedia( QMediaContent() ); // Forget about audioBuffer. player.setMedia( QMediaContent() ); // Forget about audioBuffer.
#endif #endif
audioBuffer.close(); if( audioBuffer )
audioBuffer.setData( QByteArray() ); // Free memory. {
audioBuffer->close();
audioBuffer->setData( QByteArray() ); // Free memory.
audioBuffer.clear();
}
} }
void MultimediaAudioPlayer::onMediaPlayerError() void MultimediaAudioPlayer::onMediaPlayerError()

View file

@ -12,6 +12,7 @@
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) #if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
#include <QAudioOutput> #include <QAudioOutput>
#endif #endif
#include <QPointer>
class MultimediaAudioPlayer : public AudioPlayerInterface class MultimediaAudioPlayer : public AudioPlayerInterface
{ {
@ -26,7 +27,7 @@ private slots:
void onMediaPlayerError(); void onMediaPlayerError();
private: private:
QBuffer audioBuffer; QPointer<QBuffer> audioBuffer;
QMediaPlayer player; ///< Depends on audioBuffer. QMediaPlayer player; ///< Depends on audioBuffer.
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) #if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
QAudioOutput audioOutput; QAudioOutput audioOutput;