forked from ghostfox/qyouradio
heugh jazz
This commit is contained in:
127
player.cpp
127
player.cpp
@@ -1,15 +1,59 @@
|
||||
#include "player.h"
|
||||
|
||||
// Public
|
||||
// HOLY GRAIL
|
||||
// https://doc.qt.io/qt-6/qaudiobufferoutput.htmli t
|
||||
|
||||
Player::Player(QObject *parent) : QObject(parent),
|
||||
player(this), output(this), manager(this)
|
||||
{
|
||||
QObject::connect(&this->player, &QMediaPlayer::mediaStatusChanged, this, [this] () {
|
||||
emit this->hasVideoChanged();
|
||||
emit this->loadingChanged();
|
||||
const QMediaPlayer::MediaStatus status = this->player.mediaStatus();
|
||||
if (status == QMediaPlayer::NoMedia)
|
||||
{
|
||||
qDebug("Player::Player(): NoMedia");
|
||||
this->stop();
|
||||
}
|
||||
else if (status == QMediaPlayer::BufferingMedia || status == QMediaPlayer::LoadingMedia)
|
||||
{
|
||||
qDebug("Player::Player() loadingChanged");
|
||||
this->m_loading = true;
|
||||
emit this->loadingChanged();
|
||||
}
|
||||
else if (status == QMediaPlayer::EndOfMedia)
|
||||
{
|
||||
qDebug("Player::Player() playingChanged");
|
||||
emit this->playingChanged();
|
||||
}
|
||||
else
|
||||
{
|
||||
this->m_loading = false;
|
||||
emit this->loadingChanged();
|
||||
if (!this->m_initialLoadFinished)
|
||||
{
|
||||
this->m_initialLoadFinished = true;
|
||||
this->play();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
QObject::connect(&this->player, &QMediaPlayer::positionChanged, this, [this] () {
|
||||
this->m_position = this->player.position();
|
||||
emit this->positionChanged();
|
||||
});
|
||||
|
||||
QObject::connect(&this->player, &QMediaPlayer::bufferProgressChanged, this, [this] () {
|
||||
this->m_buffered = this->player.bufferedTimeRange().latestTime() / this->m_duration;
|
||||
emit this->bufferedChanged();
|
||||
});
|
||||
|
||||
QObject::connect(&this->player, &QMediaPlayer::errorOccurred, this, [this] () {
|
||||
this->m_failed = true;
|
||||
qDebug("Player::Player() failedChanged");
|
||||
emit this->failedChanged();
|
||||
});
|
||||
|
||||
QObject::connect(&this->player, &QMediaPlayer::playbackStateChanged, this, [this] () {
|
||||
qDebug("Player::Player() playingChanged");
|
||||
emit this->playingChanged();
|
||||
});
|
||||
|
||||
@@ -28,20 +72,10 @@ 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;
|
||||
}
|
||||
// ********
|
||||
// Read
|
||||
// ********
|
||||
|
||||
bool Player::playing() const
|
||||
{
|
||||
@@ -53,15 +87,29 @@ float Player::volume() const
|
||||
return this->output.volume();
|
||||
}
|
||||
|
||||
// *********
|
||||
// Write
|
||||
// *********
|
||||
|
||||
void Player::setVolume(float newVolume)
|
||||
{
|
||||
this->output.setVolume(newVolume);
|
||||
}
|
||||
|
||||
// Public slots
|
||||
void Player::setPosition(float newPosition)
|
||||
{
|
||||
this->player.setPosition(newPosition);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ***********
|
||||
// Methods
|
||||
// ***********
|
||||
|
||||
void Player::loadVideo(QString id)
|
||||
{
|
||||
if (this->hasVideo())
|
||||
if (this->m_active)
|
||||
{
|
||||
this->unloadVideo();
|
||||
}
|
||||
@@ -82,8 +130,32 @@ void Player::loadVideo(QString id)
|
||||
|
||||
this->player.setSource(QUrl("https://youvideo.nonamesoft.xyz/youvideo/api/videofile/with_extension/" + id + data ["extension"].toString()));
|
||||
|
||||
emit this->hasVideoChanged();
|
||||
this->m_title = data["name"].toString();
|
||||
emit this->titleChanged();
|
||||
|
||||
this->m_description = data["description"].toString();
|
||||
emit this->descriptionChanged();
|
||||
|
||||
this->m_id = id;
|
||||
emit this->idChanged();
|
||||
|
||||
this->m_initialLoadFinished = false;
|
||||
|
||||
this->m_active = true;
|
||||
emit this->activeChanged();
|
||||
|
||||
this->m_duration = data["metadata"]["duration"].toDouble() * 1000;
|
||||
emit this->durationChanged();
|
||||
|
||||
this->m_position = 0;
|
||||
emit this->positionChanged();
|
||||
|
||||
this->m_buffered = 0;
|
||||
emit this->bufferedChanged();
|
||||
|
||||
this->m_loading = true;
|
||||
emit this->loadingChanged();
|
||||
|
||||
this->m_failed = false;
|
||||
emit this->failedChanged();
|
||||
|
||||
@@ -97,17 +169,14 @@ 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();
|
||||
emit this->activeChanged();
|
||||
this->m_active = false;
|
||||
emit this->activeChanged();
|
||||
}
|
||||
|
||||
void Player::play()
|
||||
{
|
||||
qDebug() << "Player::play(): Can't play video?" << this->hasVideo() << this->loading();
|
||||
if (!this->hasVideo() || this->loading())
|
||||
if (!this->m_active || this->m_loading)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -116,8 +185,7 @@ void Player::play()
|
||||
|
||||
void Player::pause()
|
||||
{
|
||||
qDebug() << "Player::pause(): Can't pause video?" << (!this->hasVideo() || this->loading());
|
||||
if (!this->hasVideo() || this->loading())
|
||||
if (!this->m_active || this->m_loading)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -126,8 +194,7 @@ void Player::pause()
|
||||
|
||||
void Player::stop()
|
||||
{
|
||||
qDebug() << "Player::stop(): Can't stop video?" << (!this->hasVideo() || this->loading());
|
||||
if (!this->hasVideo() || this->loading())
|
||||
if (!this->m_active || this->m_loading)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user