From f8d9b347a823b8e3c4e506b5570cd8b982e30fd7 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Mon, 6 Apr 2020 20:23:29 +0800 Subject: [PATCH] basic function with QtMultimedia stuff... --- CMakeLists.txt | 4 +- icons/audio-volume-muted.png | Bin 0 -> 18760 bytes icons/media-playback-pause.png | Bin 0 -> 18453 bytes mainwindow.cpp | 152 ++++++++++++++++++++++++++++++++- mainwindow.h | 20 +++++ mainwindow.ui | 14 ++- resources.qrc | 2 + 7 files changed, 186 insertions(+), 6 deletions(-) create mode 100644 icons/audio-volume-muted.png create mode 100644 icons/media-playback-pause.png diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d85f48..3ec9fd7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) -find_package(Qt5 COMPONENTS Widgets REQUIRED) +find_package(Qt5 COMPONENTS Widgets Multimedia REQUIRED) add_executable(pineapple-music main.cpp @@ -23,4 +23,4 @@ add_executable(pineapple-music resources.qrc ) -target_link_libraries(pineapple-music PRIVATE Qt5::Widgets) +target_link_libraries(pineapple-music PRIVATE Qt5::Widgets Qt5::Multimedia) diff --git a/icons/audio-volume-muted.png b/icons/audio-volume-muted.png new file mode 100644 index 0000000000000000000000000000000000000000..6c7da33b0863d12325685f34f5cf70e402eee982 GIT binary patch literal 18760 zcmeI32{=^m+sBWkvP80^s7xdMQjJ*{F)?Jx@FT)lT8){(Fk3T&8I`mzDuiTBD5;@M zNVZm$Eh-5`MY4q?De6Bc=~VOn-uL~#*L(f1Yvvj=b3gZUf1h(c&;8uzIj-v*a9C|6 zC!;0<0Dzn|&cadf4H^BDnkx8Y2L-(sd`UBL?rZ>%RT}*h1L6{91AvSj#oXM%!H2)%9MCq3!^nntZ9d3hpTiM*+fa z1x3@_d-lt?D67m&z~OK0d@O@Y-2XCpah~_egZtNp;@2Cz)o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}x9 zuLKtahxX1Q!H|cvFH) zJRgWxf(rsfyeYvYo)5$;!36;#-jv`H&j;d_;DP`VZ%S~9=L7Lda6y2GHzl~l^MQCJ zxFA5pn-W~&`9Qo9To54QO$jdXd>~#4E(j3urUaLGJ`k@27X*lSQ-Vu8ABb0i3j##E zDZwS455z0M1py-76pKrK^zmGfC3+?|Nc8Y+H$pg7^x!UxLUObNfDl~(2-^k#eP2c2 ztpE^!0)RK(06;hb02=H=Yws)t04bJ@rI~YZO?%A6Ww%VH6>g5H&fo{N!LbudkF?s2AMK-;DumRR^y>oN^^0)P$ zw?`e?be~dw<_*$(cU6j}o(;~ne`V5_sHWN z-Uf3P*i|r(<6U%b%i?-7vm!e{8sEw%XqYL@P_tSoRNU7h*mk3yU~@>L-lO&+5SQ-S zmh-qRF`B0rd2cY-IB(pnsybY96c_jCm{8`~+&!l%lTnkcO+8CJr5|qzoVHWPX@#nl zQ}WqYg8sOxp0J15O}O!w78Wy?JXLpE^Ui*a#l(7GMwko-P%Mzb16pBzSWTQ$LGR$1 z(h^aH#@am7hd%fj&r4d~XRP+VyWV&~O!YFsjKa&I`J2J{%Up1S0jVL8Fd{)^^WIAv zJr-48om-4aL#&$c#D?;YZYNW)!V}ZodHrRt6!7WDqeEwR+&nItCBVjNh2=SO@2&p= DX(5A* literal 0 HcmV?d00001 diff --git a/mainwindow.cpp b/mainwindow.cpp index 4c6c0f8..de09f1b 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -2,19 +2,63 @@ #include "./ui_mainwindow.h" #include +#include +#include #include +#include +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) + , m_mediaPlayer(new QMediaPlayer(this)) { ui->setupUi(this); this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint); this->setAttribute(Qt::WA_TranslucentBackground, true); + connect(m_mediaPlayer, &QMediaPlayer::currentMediaChanged, this, [=](const QMediaContent &media) { + ui->titleLabel->setText(media.canonicalUrl().fileName()); + }); + + connect(m_mediaPlayer, &QMediaPlayer::positionChanged, this, [=](qint64 pos) { + ui->nowTimeLabel->setText(ms2str(pos)); + if (m_mediaPlayer->duration() != 0) { + ui->playbackSlider->setSliderPosition(ui->playbackSlider->maximum() * pos / m_mediaPlayer->duration()); + } + }); + + connect(m_mediaPlayer, &QMediaPlayer::mutedChanged, this, [=](bool muted) { + if (muted) { + ui->volumeBtn->setIcon(QIcon(":/icons/icons/audio-volume-muted.png")); + } else { + ui->volumeBtn->setIcon(QIcon(":/icons/icons/audio-volume-high.png")); + } + }); + + connect(m_mediaPlayer, &QMediaPlayer::durationChanged, this, [=](qint64 dua) { + ui->totalTimeLabel->setText(ms2str(dua)); + }); + + connect(m_mediaPlayer, &QMediaPlayer::stateChanged, this, [=](QMediaPlayer::State newState) { + switch (newState) { + case QMediaPlayer::PlayingState: + ui->playBtn->setIcon(QIcon(":/icons/icons/media-playback-pause.png")); + break; + case QMediaPlayer::StoppedState: + case QMediaPlayer::PausedState: + ui->playBtn->setIcon(QIcon(":/icons/icons/media-playback-start.png")); + break; + } + }); + + connect(m_mediaPlayer, &QMediaPlayer::volumeChanged, this, [=](int vol) { + ui->volumeSlider->setValue(vol); + }); + m_bgLinearGradient.setColorAt(0, QColor(255, 255, 255, 25)); // a:0 - m_bgLinearGradient.setColorAt(1, QColor(255, 255, 255, 200)); + m_bgLinearGradient.setColorAt(1, QColor(255, 255, 255, 75)); // a:200 m_bgLinearGradient.setStart(0, 0); m_bgLinearGradient.setFinalStop(0, height()); @@ -39,8 +83,13 @@ void MainWindow::paintEvent(QPaintEvent * e) { QPainter painter(this); - painter.setBrush(QBrush(m_bgLinearGradient)); painter.setPen(Qt::NoPen); + + // Temp bg + painter.setBrush(QColor(20, 32, 83)); + painter.drawRect(0, 0, width(), height()); + + painter.setBrush(QBrush(m_bgLinearGradient)); painter.drawRect(0, 0, width(), height()); return QMainWindow::paintEvent(e); @@ -74,7 +123,106 @@ void MainWindow::mouseReleaseEvent(QMouseEvent *event) return QMainWindow::mouseReleaseEvent(event); } +void MainWindow::loadFile() +{ + QStringList files = QFileDialog::getOpenFileNames(this, + tr("Select songs to play"), + QDir::homePath(), + tr("Audio Files") + " (*.mp3 *.wav *.aiff *.ape *.flac *.ogg *.oga)"); + QMediaPlaylist * playlist = new QMediaPlaylist(m_mediaPlayer); + playlist->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop); + for (const QString & fileName : files) { + bool succ = playlist->addMedia(QMediaContent(fileName)); + if (!succ) { + qDebug("!!!!!!!!! break point time !!!!!!!!!"); + } + } + + m_mediaPlayer->setPlaylist(playlist); +} + void MainWindow::on_closeWindowBtn_clicked() { m_fadeOutAnimation->start(); } + +void MainWindow::on_playBtn_clicked() +{ + if (m_mediaPlayer->mediaStatus() == QMediaPlayer::NoMedia) { + loadFile(); + m_mediaPlayer->play(); + } else { + if (QList {QMediaPlayer::PausedState, QMediaPlayer::StoppedState} + .contains(m_mediaPlayer->state())) { + m_mediaPlayer->play(); + } else { + m_mediaPlayer->pause(); + } + } +} + +QString MainWindow::ms2str(qint64 ms) +{ + QTime duaTime(QTime::fromMSecsSinceStartOfDay(ms)); + if (duaTime.hour() > 0) { + return duaTime.toString("h:mm:ss"); + } else { + return duaTime.toString("m:ss"); + } +} + +void MainWindow::on_volumeSlider_valueChanged(int value) +{ + if (m_mediaPlayer->isMuted()) { + m_mediaPlayer->setMuted(false); + } + m_mediaPlayer->setVolume(value); +} + +void MainWindow::on_stopBtn_clicked() +{ + m_mediaPlayer->stop(); +} + +void MainWindow::on_playbackSlider_valueChanged(int value) +{ + qint64 currPos = m_mediaPlayer->duration() == 0 ? value : m_mediaPlayer->position() * ui->playbackSlider->maximum() / m_mediaPlayer->duration(); + if (qAbs(currPos - value) > 2) { + m_mediaPlayer->setPosition(ui->playbackSlider->value() * 1.0 / ui->playbackSlider->maximum() * m_mediaPlayer->duration()); + } +} + +void MainWindow::on_prevBtn_clicked() +{ + // QMediaPlaylist::previous() won't work when in CurrentItemInLoop playmode, + // and also works not as intended when in other playmode, so do it manually... + QMediaPlaylist * playlist = m_mediaPlayer->playlist(); + if (playlist) { + int index = playlist->currentIndex(); + int count = playlist->mediaCount(); + + m_mediaPlayer->playlist()->setCurrentIndex(index == 0 ? count - 1 : index - 1); + } +} + +void MainWindow::on_nextBtn_clicked() +{ + // see also: MainWindow::on_prevBtn_clicked() + QMediaPlaylist * playlist = m_mediaPlayer->playlist(); + if (playlist) { + int index = playlist->currentIndex(); + int count = playlist->mediaCount(); + + m_mediaPlayer->playlist()->setCurrentIndex(index == (count - 1) ? 0 : index + 1); + } +} + +void MainWindow::on_volumeBtn_clicked() +{ + m_mediaPlayer->setMuted(!m_mediaPlayer->isMuted()); +} + +void MainWindow::on_minimumWindowBtn_clicked() +{ + this->showMinimized(); +} diff --git a/mainwindow.h b/mainwindow.h index 6f9b7ec..804cfb6 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -6,6 +6,7 @@ QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } +class QMediaPlayer; class QPropertyAnimation; QT_END_NAMESPACE @@ -24,16 +25,35 @@ protected: void mouseMoveEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override; + void loadFile(); + private slots: void on_closeWindowBtn_clicked(); + void on_playBtn_clicked(); + void on_volumeSlider_valueChanged(int value); + void on_stopBtn_clicked(); + + void on_playbackSlider_valueChanged(int value); + + void on_prevBtn_clicked(); + + void on_nextBtn_clicked(); + + void on_volumeBtn_clicked(); + + void on_minimumWindowBtn_clicked(); private: QPoint m_oldMousePos; bool m_clickedOnWindow = false; + bool m_playbackSliderPressed = false; QLinearGradient m_bgLinearGradient; Ui::MainWindow *ui; + QMediaPlayer *m_mediaPlayer; QPropertyAnimation *m_fadeOutAnimation; + + static QString ms2str(qint64 ms); }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index edf7775..fef3512 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -88,7 +88,11 @@ QPushButton#closeWindowBtn:hover { /****** Label ******/ -QLabel#coverLabel{ +QLabel { + color: white; +} + +QLabel#coverLabel { border: 1px solid grey; } @@ -280,6 +284,9 @@ QLabel#coverLabel{ + + 1000 + Qt::Horizontal @@ -504,8 +511,11 @@ QLabel#coverLabel{ 32 + + 100 + - 99 + 100 Qt::Horizontal diff --git a/resources.qrc b/resources.qrc index 98f22de..7eb5e16 100644 --- a/resources.qrc +++ b/resources.qrc @@ -9,5 +9,7 @@ icons/view-media-playlist.png icons/window-close.png icons/window-minimize.png + icons/media-playback-pause.png + icons/audio-volume-muted.png