forked from ghostfox/qyouradio
fist comin
This commit is contained in:
131
player.cpp
131
player.cpp
@@ -2,32 +2,21 @@
|
||||
|
||||
// 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}
|
||||
})
|
||||
player(this), output(this), manager(this)
|
||||
{
|
||||
QObject::connect(&this->player, &QMediaPlayer::mediaStatusChanged, this, [this] () {
|
||||
emit this->hasVideoChanged();
|
||||
emit this->loadingChanged();
|
||||
});
|
||||
|
||||
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);
|
||||
this->player.setAudioOutput(&this->output);
|
||||
|
||||
@@ -39,12 +28,26 @@ Player::~Player()
|
||||
qDebug("Player::~Player(): Destructed");
|
||||
}
|
||||
|
||||
bool Player::hasVideo() const
|
||||
{
|
||||
return this->player.isAvailable();
|
||||
}
|
||||
|
||||
bool Player::loading() const
|
||||
{
|
||||
return this->player.mediaStatus() == QMediaPlayer::LoadingMedia || this->player.mediaStatus() == QMediaPlayer::BufferingMedia;
|
||||
}
|
||||
|
||||
bool Player::failed() const
|
||||
{
|
||||
return this->m_failed;
|
||||
}
|
||||
|
||||
bool Player::playing() const
|
||||
{
|
||||
return this->player.playbackState() == QMediaPlayer::PlayingState;
|
||||
}
|
||||
|
||||
|
||||
float Player::volume() const
|
||||
{
|
||||
return this->output.volume();
|
||||
@@ -55,59 +58,67 @@ 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];
|
||||
}
|
||||
|
||||
// Public slots
|
||||
void Player::startPlaying(u_int8_t index)
|
||||
void Player::loadVideo(QString id)
|
||||
{
|
||||
if (index < 0 || index >= this->m_stations.length())
|
||||
if (this->hasVideo())
|
||||
{
|
||||
qWarning("Player::startPlaying(): Tried accessing out of bounds.");
|
||||
this->unloadVideo();
|
||||
}
|
||||
const QNetworkReply* reply = this->manager.get(QNetworkRequest(QUrl("https://youvideo.nonamesoft.xyz/youvideo/video/" + id)));
|
||||
if (reply->error() != QNetworkReply::NoError)
|
||||
{
|
||||
qDebug() << "Player::loadVideo(): Video " << id << " doesn't exist!\n";
|
||||
delete reply;
|
||||
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));
|
||||
this->player.play();
|
||||
qDebug("Player::startPlaying(): Starting playing");
|
||||
qDebug() << "Player::fetchMetadata(): Yippie! Got video " << id << "\n";
|
||||
|
||||
this->player.setSource(QUrl("https://youvideo.nonamesoft.xyz/youvideo/api/" + id));
|
||||
|
||||
// Signals will be sent by shit sent from player
|
||||
this->m_failed = false;
|
||||
emit this->failedChanged();
|
||||
|
||||
delete reply;
|
||||
}
|
||||
|
||||
void Player::stopPlaying()
|
||||
void Player::unloadVideo() {
|
||||
this->player.setSource(QUrl(""));
|
||||
this->player.stop();
|
||||
this->m_failed = false;
|
||||
|
||||
emit this->hasVideoChanged();
|
||||
emit this->failedChanged();
|
||||
emit this->loadingChanged();
|
||||
emit this->playingChanged();
|
||||
}
|
||||
|
||||
void Player::play()
|
||||
{
|
||||
if (!this->playing())
|
||||
if (!this->hasVideo() || !this->loading())
|
||||
{
|
||||
return;
|
||||
}
|
||||
this->player.play();
|
||||
}
|
||||
|
||||
void Player::pause()
|
||||
{
|
||||
if (!this->hasVideo() || !this->loading())
|
||||
{
|
||||
return;
|
||||
}
|
||||
this->player.pause();
|
||||
}
|
||||
|
||||
void Player::stop()
|
||||
{
|
||||
if (!this->hasVideo() || !this->loading())
|
||||
{
|
||||
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();
|
||||
|
||||
qDebug("Player::stopPlaying(): Stopping playback...");
|
||||
this->unloadVideo();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user