From b01dfe17fdd6b0a1b92fec6636c74c6794cc3fbb Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Thu, 12 Sep 2024 21:36:54 +0800 Subject: [PATCH] fixes: follow system default audio output device... ...and also: - fix crash when triggered the open file dialog to select music files but clicked cancel. - support gif and jp*e*g suffix for drag and drop for skin selection --- mainwindow.cpp | 14 +++++++++++--- mainwindow.h | 2 ++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index 36773b9..cf4fdb0 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -26,6 +26,8 @@ #include #include #include +#include +#include constexpr QSize miniSize(490, 160); constexpr QSize fullSize(490, 420); @@ -33,12 +35,12 @@ constexpr QSize fullSize(490, 420); MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) + , m_mediaDevices(new QMediaDevices(this)) , m_mediaPlayer(new QMediaPlayer(this)) , m_audioOutput(new QAudioOutput(this)) , m_playlistManager(new PlaylistManager(this)) { ui->setupUi(this); - m_playlistManager->setAutoLoadFilterSuffixes({ "*.mp3", "*.wav", "*.aiff", "*.ape", "*.flac", "*.ogg", "*.oga", "*.mpga" }); @@ -207,7 +209,8 @@ void MainWindow::dropEvent(QDropEvent *e) return; } - if (fileName.endsWith(".png") || fileName.endsWith(".jpg")) { + if (fileName.endsWith(".png") || fileName.endsWith(".jpg") || + fileName.endsWith(".jpeg") || fileName.endsWith(".gif")) { setSkin(fileName, true); return; } @@ -227,6 +230,7 @@ void MainWindow::loadFile() tr("Select songs to play"), musicFolders.first(), tr("Audio Files") + " (*.mp3 *.wav *.aiff *.ape *.flac *.ogg *.oga)"); + if (files.isEmpty()) return; QList urlList; for (const QString & fileName : files) { urlList.append(QUrl::fromLocalFile(fileName)); @@ -377,6 +381,10 @@ void MainWindow::initUiAndAnimation() void MainWindow::initConnections() { + connect(m_mediaDevices, &QMediaDevices::audioOutputsChanged, this, [=]{ + m_audioOutput->setDevice(m_mediaDevices->defaultAudioOutput()); + }); + connect(m_mediaPlayer, &QMediaPlayer::sourceChanged, this, [=](){ QUrl fileUrl(m_mediaPlayer->source()); @@ -553,7 +561,7 @@ void MainWindow::on_setSkinBtn_clicked() imageFolders.append(QDir::homePath()); QString image = QFileDialog::getOpenFileName(this, tr("Select image as background skin"), imageFolders.first(), - tr("Image files (*.jpg *.jpeg *.png)")); + tr("Image files (*.jpg *.jpeg *.png *.gif)")); if(!image.isEmpty()) { setSkin(image, true); } diff --git a/mainwindow.h b/mainwindow.h index 21fc21f..19737ec 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -11,6 +11,7 @@ QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } +class QMediaDevices; class QMediaPlayer; class QAudioOutput; class QPropertyAnimation; @@ -85,6 +86,7 @@ private: Ui::MainWindow *ui; + QMediaDevices *m_mediaDevices; QMediaPlayer *m_mediaPlayer; QAudioOutput *m_audioOutput; QPropertyAnimation *m_fadeOutAnimation;