Files
Qt-Music-Player/src/windows/playerWindow/playerWindow.cpp

55 lines
1.2 KiB
C++
Raw Normal View History

#include "playerWindow.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QPixmap>
PlayerWindow::PlayerWindow(const AlbumData& item, QWidget* parent)
: QWidget(parent), item_(item)
{
this->setWindowTitle("Now Playing");
this->resize(300, 300);
QVBoxLayout* layout = new QVBoxLayout(this);
QLabel* art = new QLabel;
QPixmap pix(item.imagePath);
art->setPixmap(pix.scaled(250, 250, Qt::KeepAspectRatio, Qt::SmoothTransformation));
art->setAlignment(Qt::AlignCenter);
layout->addWidget(art);
player_ = new QMediaPlayer(this);
audio_ = new QAudioOutput(this);
player_->setAudioOutput(audio_);
connect(player_, &QMediaPlayer::mediaStatusChanged, this, [this](QMediaPlayer::MediaStatus s){
if (s == QMediaPlayer::EndOfMedia) {
playNext();
}
});
playNext();
}
void PlayerWindow::playNext()
{
if (index_ >= item_.audioFiles.size()) {
this->close();
return;
}
player_->setSource(QUrl::fromLocalFile(item_.audioFiles[index_]));
player_->play();
index_++;
}
// Stop the music when the window is closed
void PlayerWindow::closeEvent(QCloseEvent* event)
{
if (player_) {
player_->stop();
}
QWidget::closeEvent(event); // still let Qt do its normal cleanup
}