Skip to content

armgit / Spun

Public Imported from GitHub
0

A CD-shaped music player for Linux, with local playback and Cider integration.

Follow the system default audio output device

yappologisticc0778d9a4c1b
diff --git a/src/mediauitest.cpp b/src/mediauitest.cpp
index 8f8fa72..f05bb96 100644
--- a/src/mediauitest.cpp
+++ b/src/mediauitest.cpp
@@ -8,6 +8,7 @@
#include <QQuickItem>
#include <QQuickItemGrabResult>
#include <QTest>
+#include <QMediaDevices>
#include <QElapsedTimer>
#include <QDir>
#include <QFile>
@@ -141,6 +142,30 @@ int exerciseMediaUi(Player &player, QQuickWindow *window, const QString &temp, c
}
}
+ // Playback follows the system default output when that default changes.
+ {
+ player.play();
+ // The output is built on a worker, so wait for it rather than assume it exists.
+ const bool ready=wait([&]{return !player.audioDevice().isNull();});
+ if(!ready)std::cout<<"SKIP audio output checks: no output device on this host"<<std::endl;
+ else{
+ check(player.audioDevice()==QMediaDevices::defaultAudioOutput(),"output opens on the current default device");
+ auto *devices=player.findChild<QMediaDevices*>();
+ check(devices!=nullptr,"player watches the system for output device changes");
+ if(devices){
+ // Let the transport settle before asserting the change is transparent.
+ check(wait([&]{return player.playing();}),"playback runs before the output device changes");
+ QTest::qWait(200);
+ const double levelBefore=player.volume();
+ QMetaObject::invokeMethod(devices,"audioOutputsChanged");
+ QTest::qWait(200);
+ check(player.audioDevice()==QMediaDevices::defaultAudioOutput(),"a device change re-points output at the new default");
+ check(qFuzzyCompare(player.volume()+1.,levelBefore+1.),"following the default preserves the level");
+ check(player.playing(),"following the default keeps playback running");
+ }
+ }
+ player.pause();QTest::qWait(60);
+ }
// Check actual reverse pixels: valid theme properties alone missed a
// hardcoded silver backing behind light ink in the CD/cassette booklets.
{
diff --git a/src/player.cpp b/src/player.cpp
index 65fb34d..2906bca 100644
--- a/src/player.cpp
+++ b/src/player.cpp
@@ -116,6 +116,8 @@ void Player::ensureMedia() {
connect(m_media.get(), &QMediaPlayer::positionChanged, this, &Player::positionChanged);
connect(m_media.get(), &QMediaPlayer::durationChanged, this, &Player::durationChanged);
connect(m_audio.get(), &QAudioOutput::volumeChanged, this, &Player::volumeChanged);
+ // QAudioOutput samples the default device once, so follow later changes to it.
+ connect(&m_mediaDevices, &QMediaDevices::audioOutputsChanged, this, &Player::syncAudioDevice);
connect(m_media.get(), &QMediaPlayer::errorOccurred, this, [this](auto, const QString &message) {
m_restorePosition = -1;
fail("Could not play this track. " + message);
@@ -591,6 +593,13 @@ QString Player::albumKey() const {
}
void Player::attachMixer(Tx6 *mixer) { m_mixer=mixer;refreshMixerRoute(); }
+// Moving to the new default must not disturb transport, level or the mixer's mute.
+void Player::syncAudioDevice() {
+ if (!m_audio) return;
+ const auto preferred = QMediaDevices::defaultAudioOutput();
+ if (preferred.isNull() || m_audio->device() == preferred) return;
+ m_audio->setDevice(preferred);
+}
void Player::refreshMixerRoute() {
if(!m_media)return;
const bool active=m_mixer&&m_mixer->active();
diff --git a/src/player.h b/src/player.h
index d229b06..e8828cb 100644
--- a/src/player.h
+++ b/src/player.h
@@ -3,6 +3,8 @@
#include <QImage>
#include <QMediaPlayer>
#include <QAudioOutput>
+#include <QAudioDevice>
+#include <QMediaDevices>
#include <QSettings>
#include <QUrl>
#include <QVariantList>
@@ -141,6 +143,9 @@ public:
QString trackId() const;
void setVolume(double value);
void attachMixer(Tx6 *mixer);
+ // Qt binds an output device at construction; the system default can change later.
+ void syncAudioDevice();
+ QAudioDevice audioDevice() const { return m_audio ? m_audio->device() : QAudioDevice{}; }
void refreshMixerRoute();
void setShuffle(bool value);
void setRepeatMode(int value);
@@ -207,6 +212,7 @@ private:
bool m_preparingAudio = false, m_playPending = false;
double m_volume = .65;
Tx6 *m_mixer=nullptr;
+ QMediaDevices m_mediaDevices{this};
std::unique_ptr<QAudioOutput> m_audio;
std::unique_ptr<QMediaPlayer> m_media;
std::unique_ptr<QAudioBufferOutput> m_mixOutput;