Skip to content

Commit

Permalink
Merge pull request #390 from Mitricho/master
Browse files Browse the repository at this point in the history
Update qavaudiooutput.cpp
  • Loading branch information
valbok authored Aug 20, 2023
2 parents 21b3908 + 493cd55 commit 4f88dff
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions src/QtAVPlayer/qavaudiooutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <QAudioOutput>
#else
#include <QAudioSink>
#include <QMediaDevices>
#endif

extern "C" {
Expand Down Expand Up @@ -89,15 +90,18 @@ class QAVAudioOutputPrivate : public QIODevice

#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;
int bufferSize = 0;
QList<QAVAudioFrame> frames;
qint64 offset = 0;
bool quit = 0;
AudioDevice defaultAudioDevice;
mutable QMutex mutex;
QWaitCondition cond;
QThreadPool threadPool;
Expand Down Expand Up @@ -144,21 +148,35 @@ class QAVAudioOutputPrivate : public QIODevice

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) {
case QAudio::StoppedState:
if (audioOutput->error() != QAudio::NoError)
qWarning() << "QAudioOutput stopped:" << audioOutput->error();
break;
default:
break;
}
});
[&](QAudio::State state) {
switch (state) {
case QAudio::StoppedState:
if (audioOutput->error() != QAudio::NoError)
qWarning() << "QAudioOutput stopped:" << audioOutput->error();
break;
default:
break;
}
});

if (bufferSize > 0)
audioOutput->setBufferSize(bufferSize);
Expand Down

0 comments on commit 4f88dff

Please sign in to comment.