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>
This commit is contained in:
xiaoyifang 2023-10-15 23:15:45 +08:00 committed by GitHub
parent 0fbcdda72f
commit 5de4690b2d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 6 deletions

View file

@ -40,11 +40,13 @@ void Request::update()
void Request::finish() void Request::finish()
{ {
if ( !Utils::AtomicInt::loadAcquire( isFinishedFlag ) ) { if ( !Utils::AtomicInt::loadAcquire( isFinishedFlag ) ) {
isFinishedFlag.ref(); {
emit finished(); QMutexLocker _( &dataMutex );
isFinishedFlag.ref();
QMutexLocker _( &dataMutex ); cond.wakeAll();
cond.wakeAll(); }
emit finished();
} }
} }

View file

@ -7,6 +7,9 @@
#if ( QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 ) ) #if ( QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 ) )
#include <QMediaContent> #include <QMediaContent>
#endif #endif
#if ( QT_VERSION > QT_VERSION_CHECK( 6, 2, 0 ) )
#include <QAudioDevice>
#endif
#include "multimediaaudioplayer.hh" #include "multimediaaudioplayer.hh"
MultimediaAudioPlayer::MultimediaAudioPlayer() MultimediaAudioPlayer::MultimediaAudioPlayer()
@ -26,6 +29,15 @@ MultimediaAudioPlayer::MultimediaAudioPlayer()
connect( &player, &QMediaPlayer::errorChanged, this, &MultimediaAudioPlayer::onMediaPlayerError ); connect( &player, &QMediaPlayer::errorChanged, this, &MultimediaAudioPlayer::onMediaPlayerError );
#endif #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 ) 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." ); 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 );
#if ( QT_VERSION > QT_VERSION_CHECK( 6, 2, 0 ) )
audioOutput.setDevice( QMediaDevices::defaultAudioOutput() );
player.setAudioOutput( &audioOutput );
#endif
#else #else
player.setMedia( QMediaContent(), audioBuffer ); player.setMedia( QMediaContent(), audioBuffer );
#endif #endif
player.play(); player.play();
return QString(); return {};
} }
void MultimediaAudioPlayer::stop() void MultimediaAudioPlayer::stop()

View file

@ -12,6 +12,9 @@
#if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) ) #if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) )
#include <QAudioOutput> #include <QAudioOutput>
#endif #endif
#if ( QT_VERSION >= QT_VERSION_CHECK( 6, 2, 0 ) )
#include <QMediaDevices>
#endif
#include <QPointer> #include <QPointer>
class MultimediaAudioPlayer: public AudioPlayerInterface class MultimediaAudioPlayer: public AudioPlayerInterface
@ -26,13 +29,18 @@ public:
private slots: private slots:
void onMediaPlayerError(); void onMediaPlayerError();
void audioOutputChange();
private: private:
QPointer< 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, 2, 0 ) )
QAudioOutput audioOutput; QAudioOutput audioOutput;
#endif #endif
#if ( QT_VERSION >= QT_VERSION_CHECK( 6, 2, 0 ) )
QMediaDevices mediaDevices;
#endif
}; };
#endif // MAKE_QTMULTIMEDIA_PLAYER #endif // MAKE_QTMULTIMEDIA_PLAYER