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

75 lines
1.9 KiB
C++

#include "playerWindow.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QPixmap>
#include <QFileInfo>
PlayerWindow::PlayerWindow(const AlbumData& item, QWidget* parent)
: QWidget(parent), item_(item)
{
this->setWindowTitle("Now Playing");
this->resize(300, 350);
QVBoxLayout* layout = new QVBoxLayout(this);
// Album Art
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);
// Track Label (new)
trackLabel_ = new QLabel("Track 1");
trackLabel_->setAlignment(Qt::AlignCenter);
trackLabel_->setStyleSheet("font-size: 14px; font-weight: 500;");
layout->addWidget(trackLabel_);
// Player setup
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;
}
QString filePath = item_.audioFiles[index_];
QString fileName = QFileInfo(filePath).fileName();
// Remove extension for cleaner title
QString title = cleanTrackTitle(fileName);
// Track number is index + 1
int trackNumber = index_ + 1;
trackLabel_->setText(QString("Track %1: %2").arg(trackNumber).arg(title));
player_->setSource(QUrl::fromLocalFile(filePath));
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
}