better and better

This commit is contained in:
Dark Steveneq
2025-10-20 02:53:30 +02:00
parent c168086601
commit 5ac6248c66
8 changed files with 114 additions and 125 deletions

View File

@@ -1,17 +1,17 @@
#include "player.h"
// HOLY GRAIL
// https://doc.qt.io/qt-6/qaudiobufferoutput.htmli t
// https://doc.qt.io/qt-6/qaudiobufferoutput.html
Player::Player(QObject *parent) : QObject(parent),
player(this), output(this), manager(this)
player(this), output(this)
{
QObject::connect(&this->player, &QMediaPlayer::mediaStatusChanged, this, [this] () {
const QMediaPlayer::MediaStatus status = this->player.mediaStatus();
if (status == QMediaPlayer::NoMedia)
{
qDebug("Player::Player(): NoMedia");
this->stop();
this->unloadVideo();
}
else if (status == QMediaPlayer::BufferingMedia || status == QMediaPlayer::LoadingMedia)
{
@@ -26,13 +26,12 @@ Player::Player(QObject *parent) : QObject(parent),
}
else
{
if (this->m_position == 0)
{
this->player.play();
}
this->m_loading = false;
emit this->loadingChanged();
if (!this->m_initialLoadFinished)
{
this->m_initialLoadFinished = true;
this->play();
}
}
});
@@ -41,10 +40,10 @@ Player::Player(QObject *parent) : QObject(parent),
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::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;
@@ -107,47 +106,28 @@ void Player::setPosition(float newPosition)
// Methods
// ***********
void Player::loadVideo(QString id)
// For QYouRadio:
// QEventLoop loop(this);
// QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
// loop.exec();
// const QJsonDocument data = QJsonDocument::fromJson(reply->readAll());
void Player::loadVideo(QString id, QString extension, QString title, float position)
{
if (this->m_active)
if (this->m_id != id)
{
this->unloadVideo();
}
QNetworkReply* reply = this->manager.get(QNetworkRequest(QUrl("https://youvideo.nonamesoft.xyz/youvideo/video/" + id)));
QEventLoop loop(this);
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
if (reply->error() != QNetworkReply::NoError)
{
qDebug() << "Player::loadVideo(): Video " << id << " doesn't exist!\n";
delete reply;
return;
}
const QJsonDocument data = QJsonDocument::fromJson(reply->readAll());
this->player.setSource(QUrl("https://youvideo.nonamesoft.xyz/youvideo/api/videofile/with_extension/" + id + data ["extension"].toString()));
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->player.setSource(QUrl("https://youvideo.nonamesoft.xyz/youvideo/api/videofile/with_extension/" + id + extension));
this->m_active = true;
emit this->activeChanged();
this->m_title = title;
emit this->titleChanged();
this->m_duration = data["metadata"]["duration"].toDouble() * 1000;
emit this->durationChanged();
this->m_position = 0;
this->m_position = position;
emit this->positionChanged();
this->m_buffered = 0;
@@ -159,24 +139,31 @@ void Player::loadVideo(QString id)
this->m_failed = false;
emit this->failedChanged();
qDebug() << "Player::loadVideo(): Loaded video https://youvideo.nonamesoft.xyz/youvideo/api/videofile/with_extension/" + id + data ["extension"].toString();
delete reply;
qDebug() << "Player::loadVideo(): Loaded video https://youvideo.nonamesoft.xyz/youvideo/api/videofile/with_extension/" + id + extension;
}
void Player::unloadVideo() {
qDebug("Player::unloadVideo(): Video unloaded");
this->m_id = "";
emit this->idChanged();
this->player.setSource(QUrl(""));
this->player.stop();
this->m_title = "";
emit this->titleChanged();
this->m_loading = false;
emit this->loadingChanged();
this->m_failed = false;
emit this->activeChanged();
this->m_active = false;
emit this->activeChanged();
emit this->failedChanged();
}
void Player::play()
{
if (!this->m_active || this->m_loading)
if (this->m_id.length() == 0 || this->m_loading)
{
return;
}
@@ -185,19 +172,10 @@ void Player::play()
void Player::pause()
{
if (!this->m_active || this->m_loading)
if (this->m_id.length() == 0 || this->m_loading)
{
return;
}
this->player.pause();
}
void Player::stop()
{
if (!this->m_active || this->m_loading)
{
return;
}
this->player.stop();
this->unloadVideo();
}