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
This commit is contained in:
Gary Wang 2024-09-12 21:36:54 +08:00
parent 25eed8066b
commit b01dfe17fd
No known key found for this signature in database
GPG Key ID: 5D30A4F15EA78760
2 changed files with 13 additions and 3 deletions

View File

@ -26,6 +26,8 @@
#include <QMimeData> #include <QMimeData>
#include <QWindow> #include <QWindow>
#include <QStandardPaths> #include <QStandardPaths>
#include <QMediaDevices>
#include <QAudioDevice>
constexpr QSize miniSize(490, 160); constexpr QSize miniSize(490, 160);
constexpr QSize fullSize(490, 420); constexpr QSize fullSize(490, 420);
@ -33,12 +35,12 @@ constexpr QSize fullSize(490, 420);
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
, ui(new Ui::MainWindow) , ui(new Ui::MainWindow)
, m_mediaDevices(new QMediaDevices(this))
, m_mediaPlayer(new QMediaPlayer(this)) , m_mediaPlayer(new QMediaPlayer(this))
, m_audioOutput(new QAudioOutput(this)) , m_audioOutput(new QAudioOutput(this))
, m_playlistManager(new PlaylistManager(this)) , m_playlistManager(new PlaylistManager(this))
{ {
ui->setupUi(this); ui->setupUi(this);
m_playlistManager->setAutoLoadFilterSuffixes({ m_playlistManager->setAutoLoadFilterSuffixes({
"*.mp3", "*.wav", "*.aiff", "*.ape", "*.flac", "*.ogg", "*.oga", "*.mpga" "*.mp3", "*.wav", "*.aiff", "*.ape", "*.flac", "*.ogg", "*.oga", "*.mpga"
}); });
@ -207,7 +209,8 @@ void MainWindow::dropEvent(QDropEvent *e)
return; return;
} }
if (fileName.endsWith(".png") || fileName.endsWith(".jpg")) { if (fileName.endsWith(".png") || fileName.endsWith(".jpg") ||
fileName.endsWith(".jpeg") || fileName.endsWith(".gif")) {
setSkin(fileName, true); setSkin(fileName, true);
return; return;
} }
@ -227,6 +230,7 @@ void MainWindow::loadFile()
tr("Select songs to play"), tr("Select songs to play"),
musicFolders.first(), musicFolders.first(),
tr("Audio Files") + " (*.mp3 *.wav *.aiff *.ape *.flac *.ogg *.oga)"); tr("Audio Files") + " (*.mp3 *.wav *.aiff *.ape *.flac *.ogg *.oga)");
if (files.isEmpty()) return;
QList<QUrl> urlList; QList<QUrl> urlList;
for (const QString & fileName : files) { for (const QString & fileName : files) {
urlList.append(QUrl::fromLocalFile(fileName)); urlList.append(QUrl::fromLocalFile(fileName));
@ -377,6 +381,10 @@ void MainWindow::initUiAndAnimation()
void MainWindow::initConnections() void MainWindow::initConnections()
{ {
connect(m_mediaDevices, &QMediaDevices::audioOutputsChanged, this, [=]{
m_audioOutput->setDevice(m_mediaDevices->defaultAudioOutput());
});
connect(m_mediaPlayer, &QMediaPlayer::sourceChanged, this, [=](){ connect(m_mediaPlayer, &QMediaPlayer::sourceChanged, this, [=](){
QUrl fileUrl(m_mediaPlayer->source()); QUrl fileUrl(m_mediaPlayer->source());
@ -553,7 +561,7 @@ void MainWindow::on_setSkinBtn_clicked()
imageFolders.append(QDir::homePath()); imageFolders.append(QDir::homePath());
QString image = QFileDialog::getOpenFileName(this, tr("Select image as background skin"), QString image = QFileDialog::getOpenFileName(this, tr("Select image as background skin"),
imageFolders.first(), imageFolders.first(),
tr("Image files (*.jpg *.jpeg *.png)")); tr("Image files (*.jpg *.jpeg *.png *.gif)"));
if(!image.isEmpty()) { if(!image.isEmpty()) {
setSkin(image, true); setSkin(image, true);
} }

View File

@ -11,6 +11,7 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; } namespace Ui { class MainWindow; }
class QMediaDevices;
class QMediaPlayer; class QMediaPlayer;
class QAudioOutput; class QAudioOutput;
class QPropertyAnimation; class QPropertyAnimation;
@ -85,6 +86,7 @@ private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
QMediaDevices *m_mediaDevices;
QMediaPlayer *m_mediaPlayer; QMediaPlayer *m_mediaPlayer;
QAudioOutput *m_audioOutput; QAudioOutput *m_audioOutput;
QPropertyAnimation *m_fadeOutAnimation; QPropertyAnimation *m_fadeOutAnimation;