Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update qavaudiooutput.cpp #390

Merged
merged 5 commits into from
Aug 20, 2023
Merged

Update qavaudiooutput.cpp #390

merged 5 commits into from
Aug 20, 2023

Conversation

Mitricho
Copy link
Contributor

With that improvement audioOutput properly switches to active audio output device.

With that improvement audioOutput properly switches to active audio output device.
@valbok
Copy link
Owner

valbok commented Aug 19, 2023

Hi, thank you for contribution. Could you please explain a bit more what issues you are experiencing ?

@@ -147,7 +147,7 @@ class QAVAudioOutputPrivate : public QIODevice
if (!audioOutput || (fmt.isValid() && audioOutput->format() != fmt) || audioOutput->state() == QAudio::StoppedState) {
if (audioOutput)
audioOutput->deleteLater();
audioOutput = new AudioOutput(fmt);
audioOutput = new AudioOutput(QAudioDevice(QMediaDevices::defaultAudioOutput()),fmt);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not compatible with Qt5

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it mean that default audio device is not used? But should it be used even if it is not provided?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've implemented a simple video player and have noticed the following. In case there is more than one audio device connected to the computer QtAVPlayer cannot switch to the default device automatically. I.e. when I run the player with my embedded speakers on and I connect my headphones I hear nothing. My suggestion is to add an explicit reference to the default audio output. Tested on Manjaro Linux.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think QtAVPlayer should switch to the default audio device every time it changes.

Copy link
Owner

@valbok valbok Aug 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you try this? If it works, could you just edit the PR by this change?

diff --git a/src/QtAVPlayer/qavaudiooutput.cpp b/src/QtAVPlayer/qavaudiooutput.cpp
index 7ced332..3890801 100644
--- a/src/QtAVPlayer/qavaudiooutput.cpp
+++ b/src/QtAVPlayer/qavaudiooutput.cpp
@@ -17,6 +17,7 @@
 #include <QAudioOutput>
 #else
 #include <QAudioSink>
+#include <QMediaDevices>
 #endif
 
 extern "C" {
@@ -89,8 +90,10 @@ public:
 
 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
     using AudioOutput = QAudioOutput;
+    using AudioDevice = QAudioDeviceInfo;
 #else
     using AudioOutput = QAudioSink;
+    using AudioDevice = QAudioDevice;
 #endif
     AudioOutput *audioOutput = nullptr;
     qreal volume = 1.0;
@@ -98,6 +101,7 @@ public:
     QList<QAVAudioFrame> frames;
     qint64 offset = 0;
     bool quit = 0;
+    AudioDevice defaultAudioDevice;
     mutable QMutex mutex;
     QWaitCondition cond;
     QThreadPool threadPool;
@@ -144,10 +148,24 @@ public:
 
     void init(const QAudioFormat &fmt)
     {
-        if (!audioOutput || (fmt.isValid() && audioOutput->format() != fmt) || audioOutput->state() == QAudio::StoppedState) {
-            if (audioOutput)
+#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
+        auto audioDevice = QAudioDeviceInfo::defaultOutputDevice();
+#else
+        auto audioDevice = QMediaDevices::defaultAudioOutput();
+#endif
+        if (!audioOutput
+            || (fmt.isValid() && audioOutput->format() != fmt)
+            || audioOutput->state() == QAudio::StoppedState
+            || defaultAudioDevice != audioDevice)
+        {
+            if (audioOutput) {
+                audioOutput->stop();
                 audioOutput->deleteLater();
-            audioOutput = new AudioOutput(fmt);
+            }
+
+            audioOutput = new AudioOutput(audioDevice, fmt);
+            defaultAudioDevice = audioDevice;
+
             QObject::connect(audioOutput, &AudioOutput::stateChanged, audioOutput,
                 [&](QAudio::State state) {
                     switch (state) {

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this by

  1. Starting playing
  2. Unplug headset
  3. Plug the headset back --- should hear sound.

@valbok valbok merged commit 4f88dff into valbok:master Aug 20, 2023
5 checks passed
@Mitricho
Copy link
Contributor Author

I can confirm, now QtAVPlayer switches itself to the current active audio output device. Tested with Qt6 (6.5) - can successfully switch from embedded speakers to headset.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants