Files
qyouvideo/player.cpp

114 lines
2.9 KiB
C++
Raw Normal View History

2025-10-16 19:21:35 +02:00
#include "player.h"
2025-10-16 23:20:10 +02:00
// Public
Player::Player(QObject *parent) : QObject(parent),
player(this), output(this), timer(this), m_currentIndex(0), m_stations({
Station{"Autoradio", "autoradio", "", 0},
Station{"Live Mix", "live", "", 0},
Station{"Bass Boosted", "bassboosted", "", 0}
})
2025-10-16 19:21:35 +02:00
{
2025-10-16 23:20:10 +02:00
QObject::connect(&this->player, &QMediaPlayer::playbackStateChanged, this, [this] () {
qDebug("Player::Player()->Lambda: Playing got changed!");
emit this->playingChanged();
});
QObject::connect(&this->output, &QAudioOutput::volumeChanged, this, [this] () {
qDebug("Player::Player()->Lambda: Volume got changed!");
emit this->volumeChanged();
});
this->timer.start(metadataFetchTimeout);
QObject::connect(&this->timer, &QTimer::timeout, this, [this] () {
qDebug("Player::Player()->Lambda: Timer triggered!");
if (this->m_currentIndex > -1)
{
emit this->currentStationChanged();
}
emit this->stationsChanged();
});
this->output.setVolume(0.2);
2025-10-16 23:20:10 +02:00
this->player.setAudioOutput(&this->output);
2025-10-16 19:21:35 +02:00
qDebug("Player::Player(): Constructed");
}
Player::~Player()
{
qDebug("Player::~Player(): Destructed");
}
2025-10-16 23:20:10 +02:00
bool Player::playing() const
{
return this->player.playbackState() == QMediaPlayer::PlayingState;
}
float Player::volume() const
{
return this->output.volume();
}
void Player::setVolume(float newVolume)
{
this->output.setVolume(newVolume);
}
const QList<Station> Player::stations() const
{
return this->m_stations;
}
const int Player::currentIndex() const
{
return this->m_currentIndex;
}
const Station* Player::currentStation() const
{
if (this->m_currentIndex < 0 || this->m_currentIndex >= this->m_stations.length())
{
qWarning("Player::currentStation(): Tried accessing out of bounds.");
return nullptr;
}
return &this->m_stations[this->m_currentIndex];
}
2025-10-16 23:20:10 +02:00
// Public slots
void Player::startPlaying(u_int8_t index)
2025-10-16 19:21:35 +02:00
{
if (index < 0 || index >= this->m_stations.length())
2025-10-16 23:20:10 +02:00
{
qWarning("Player::startPlaying(): Tried accessing out of bounds.");
return;
}
if (this->playing()) {
this->stopPlaying();
}
this->m_currentIndex = index;
emit this->currentIndexChanged();
emit this->currentStationChanged();
this->player.setSource(QUrl("https://youradio.nonamesoft.xyz/youradio/api/" + this->m_stations[index].m_slug));
2025-10-16 23:20:10 +02:00
this->player.play();
qDebug("Player::startPlaying(): Starting playing");
2025-10-16 19:21:35 +02:00
}
void Player::stopPlaying()
{
if (!this->playing())
2025-10-16 23:20:10 +02:00
{
qWarning("Player::stopPlaying(): Prevented redundant stopPlaying() call.");
return;
}
this->player.stop();
this->player.setSource(QUrl(""));
this->m_currentIndex = -1;
emit this->currentIndexChanged();
emit this->currentStationChanged();
2025-10-16 23:20:10 +02:00
qDebug("Player::stopPlaying(): Stopping playback...");
}