From 5de4690b2df58b799e85c10627f48f7885d97145 Mon Sep 17 00:00:00 2001 From: xiaoyifang <105986+xiaoyifang@users.noreply.github.com> Date: Sun, 15 Oct 2023 23:15:45 +0800 Subject: [PATCH] fix: audio device change handled (#1242) * fix: audio device change handled * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- src/dict/dictionary.cc | 10 ++++++---- src/multimediaaudioplayer.cc | 18 +++++++++++++++++- src/multimediaaudioplayer.hh | 10 +++++++++- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/dict/dictionary.cc b/src/dict/dictionary.cc index 2e65927f..a7ca9f8b 100644 --- a/src/dict/dictionary.cc +++ b/src/dict/dictionary.cc @@ -40,11 +40,13 @@ void Request::update() void Request::finish() { if ( !Utils::AtomicInt::loadAcquire( isFinishedFlag ) ) { - isFinishedFlag.ref(); - emit finished(); + { + QMutexLocker _( &dataMutex ); + isFinishedFlag.ref(); - QMutexLocker _( &dataMutex ); - cond.wakeAll(); + cond.wakeAll(); + } + emit finished(); } } diff --git a/src/multimediaaudioplayer.cc b/src/multimediaaudioplayer.cc index cbe692cf..8c6906a4 100644 --- a/src/multimediaaudioplayer.cc +++ b/src/multimediaaudioplayer.cc @@ -7,6 +7,9 @@ #if ( QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 ) ) #include #endif + #if ( QT_VERSION > QT_VERSION_CHECK( 6, 2, 0 ) ) + #include + #endif #include "multimediaaudioplayer.hh" MultimediaAudioPlayer::MultimediaAudioPlayer() @@ -26,6 +29,15 @@ MultimediaAudioPlayer::MultimediaAudioPlayer() connect( &player, &QMediaPlayer::errorChanged, this, &MultimediaAudioPlayer::onMediaPlayerError ); #endif + + #if ( QT_VERSION > QT_VERSION_CHECK( 6, 2, 0 ) ) + connect( &mediaDevices, &QMediaDevices::audioOutputsChanged, this, &MultimediaAudioPlayer::audioOutputChange ); + #endif +} + +void MultimediaAudioPlayer::audioOutputChange() +{ + qDebug() << "audio device changed"; } QString MultimediaAudioPlayer::play( const char * data, int size ) @@ -37,11 +49,15 @@ QString MultimediaAudioPlayer::play( const char * data, int size ) 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, 2, 0 ) ) + audioOutput.setDevice( QMediaDevices::defaultAudioOutput() ); + player.setAudioOutput( &audioOutput ); + #endif #else player.setMedia( QMediaContent(), audioBuffer ); #endif player.play(); - return QString(); + return {}; } void MultimediaAudioPlayer::stop() diff --git a/src/multimediaaudioplayer.hh b/src/multimediaaudioplayer.hh index c976b3af..19d83404 100644 --- a/src/multimediaaudioplayer.hh +++ b/src/multimediaaudioplayer.hh @@ -12,6 +12,9 @@ #if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) ) #include #endif + #if ( QT_VERSION >= QT_VERSION_CHECK( 6, 2, 0 ) ) + #include + #endif #include class MultimediaAudioPlayer: public AudioPlayerInterface @@ -26,13 +29,18 @@ public: private slots: void onMediaPlayerError(); + void audioOutputChange(); + private: QPointer< QBuffer > audioBuffer; QMediaPlayer player; ///< Depends on audioBuffer. - #if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) ) + #if ( QT_VERSION >= QT_VERSION_CHECK( 6, 2, 0 ) ) QAudioOutput audioOutput; #endif + #if ( QT_VERSION >= QT_VERSION_CHECK( 6, 2, 0 ) ) + QMediaDevices mediaDevices; + #endif }; #endif // MAKE_QTMULTIMEDIA_PLAYER