-
Notifications
You must be signed in to change notification settings - Fork 60
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
Conversation
With that improvement audioOutput properly switches to active audio output device.
Hi, thank you for contribution. Could you please explain a bit more what issues you are experiencing ? |
src/QtAVPlayer/qavaudiooutput.cpp
Outdated
@@ -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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested this by
- Starting playing
- Unplug headset
- Plug the headset back --- should hear sound.
Now should be compatible with Qt5. Not tested yet.
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. |
With that improvement audioOutput properly switches to active audio output device.