fix: repeat play the first played sound

This commit is contained in:
xiaoyifang 2023-02-13 09:23:13 +08:00
parent bd736d7f37
commit ac59713ed0
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 )
{
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()

View file

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