Files
qyouradio/player.cpp
2025-10-16 23:20:10 +02:00

72 lines
1.7 KiB
C++

#include "player.h"
// Public
Player::Player(QObject *parent) : QObject(parent),
player(this),
output(this)
{
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->output.setVolume(0.3);
this->player.setAudioOutput(&this->output);
qDebug("Player::Player(): Constructed");
}
Player::~Player()
{
qDebug("Player::~Player(): Destructed");
}
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);
}
// Public slots
void Player::startPlaying(u_int8_t index)
{
if (index < 0 || index >= maxStations)
{
qWarning("Player::startPlaying(): Tried accessing out of bounds.");
return;
}
if (this->playing()) {
this->stopPlaying();
}
this->player.setSource(QUrl(streamPrefix + stations[index].slug));
this->player.play();
qDebug("Player::startPlaying(): Starting playing");
}
void Player::stopPlaying()
{
if (this->playing())
{
qWarning("Player::stopPlaying(): Prevented redundant stopPlaying() call.");
return;
}
this->player.stop();
this->player.setSource(QUrl(""));
qDebug("Player::stopPlaying(): Stopping playback...");
}