2025-11-19 20:50:15 -07:00
|
|
|
#include "playerWindow.h"
|
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
#include <QLabel>
|
|
|
|
|
#include <QPixmap>
|
2025-11-19 21:20:27 -07:00
|
|
|
#include <QFileInfo>
|
2025-11-19 20:50:15 -07:00
|
|
|
|
|
|
|
|
PlayerWindow::PlayerWindow(const AlbumData& item, QWidget* parent)
|
|
|
|
|
: QWidget(parent), item_(item)
|
|
|
|
|
{
|
2025-11-19 21:20:27 -07:00
|
|
|
this->setWindowTitle("Now Playing");
|
|
|
|
|
this->resize(300, 350);
|
2025-11-19 20:50:15 -07:00
|
|
|
|
2025-11-19 21:20:27 -07:00
|
|
|
QVBoxLayout* layout = new QVBoxLayout(this);
|
2025-11-19 20:50:15 -07:00
|
|
|
|
2025-11-19 21:20:27 -07:00
|
|
|
// 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);
|
2025-11-19 20:50:15 -07:00
|
|
|
|
2025-11-19 21:20:27 -07:00
|
|
|
// Track Label (new)
|
|
|
|
|
trackLabel_ = new QLabel("Track 1");
|
|
|
|
|
trackLabel_->setAlignment(Qt::AlignCenter);
|
|
|
|
|
trackLabel_->setStyleSheet("font-size: 14px; font-weight: 500;");
|
|
|
|
|
layout->addWidget(trackLabel_);
|
2025-11-19 20:50:15 -07:00
|
|
|
|
2025-11-19 21:20:27 -07:00
|
|
|
// Player setup
|
|
|
|
|
player_ = new QMediaPlayer(this);
|
|
|
|
|
audio_ = new QAudioOutput(this);
|
|
|
|
|
player_->setAudioOutput(audio_);
|
2025-11-19 20:50:15 -07:00
|
|
|
|
2025-11-19 21:20:27 -07:00
|
|
|
connect(player_, &QMediaPlayer::mediaStatusChanged, this,
|
|
|
|
|
[this](QMediaPlayer::MediaStatus s){
|
|
|
|
|
if (s == QMediaPlayer::EndOfMedia) {
|
|
|
|
|
playNext();
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-11-19 20:50:15 -07:00
|
|
|
|
2025-11-19 21:20:27 -07:00
|
|
|
playNext();
|
2025-11-19 20:50:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PlayerWindow::playNext()
|
|
|
|
|
{
|
2025-11-19 21:20:27 -07:00
|
|
|
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_++;
|
2025-11-19 20:50:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
}
|