Files
qyouvideo/player.cpp

137 lines
3.3 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),
2025-10-18 21:40:02 +02:00
player(this), output(this), manager(this)
2025-10-16 19:21:35 +02:00
{
2025-10-18 21:40:02 +02:00
QObject::connect(&this->player, &QMediaPlayer::mediaStatusChanged, this, [this] () {
emit this->hasVideoChanged();
emit this->loadingChanged();
});
2025-10-16 23:20:10 +02:00
QObject::connect(&this->player, &QMediaPlayer::playbackStateChanged, this, [this] () {
emit this->playingChanged();
});
QObject::connect(&this->output, &QAudioOutput::volumeChanged, this, [this] () {
emit this->volumeChanged();
});
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-18 21:40:02 +02:00
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;
}
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);
}
2025-10-18 21:40:02 +02:00
// Public slots
void Player::loadVideo(QString id)
{
2025-10-18 21:40:02 +02:00
if (this->hasVideo())
{
this->unloadVideo();
}
2025-10-18 23:00:53 +02:00
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();
2025-10-18 21:40:02 +02:00
if (reply->error() != QNetworkReply::NoError)
{
qDebug() << "Player::loadVideo(): Video " << id << " doesn't exist!\n";
delete reply;
return;
}
2025-10-18 23:00:53 +02:00
const QJsonDocument data = QJsonDocument::fromJson(reply->readAll());
2025-10-18 21:40:02 +02:00
2025-10-18 23:00:53 +02:00
this->player.setSource(QUrl("https://youvideo.nonamesoft.xyz/youvideo/api/videofile/with_extension/" + id + data ["extension"].toString()));
2025-10-18 21:40:02 +02:00
2025-10-18 23:00:53 +02:00
emit this->hasVideoChanged();
emit this->loadingChanged();
2025-10-18 21:40:02 +02:00
this->m_failed = false;
emit this->failedChanged();
2025-10-18 23:00:53 +02:00
qDebug() << "Player::loadVideo(): Loaded video https://youvideo.nonamesoft.xyz/youvideo/api/videofile/with_extension/" + id + data ["extension"].toString();
2025-10-18 21:40:02 +02:00
delete reply;
}
2025-10-18 21:40:02 +02:00
void Player::unloadVideo() {
2025-10-18 23:00:53 +02:00
qDebug("Player::unloadVideo(): Video unloaded");
2025-10-18 21:40:02 +02:00
this->player.setSource(QUrl(""));
this->player.stop();
this->m_failed = false;
emit this->hasVideoChanged();
emit this->failedChanged();
emit this->loadingChanged();
emit this->playingChanged();
}
2025-10-18 21:40:02 +02:00
void Player::play()
{
2025-10-18 23:00:53 +02:00
qDebug() << "Player::play(): Can't play video?" << this->hasVideo() << this->loading();
if (!this->hasVideo() || this->loading())
{
2025-10-18 21:40:02 +02:00
return;
2025-10-18 23:00:53 +02:00
}
2025-10-18 21:40:02 +02:00
this->player.play();
}
2025-10-16 23:20:10 +02:00
2025-10-18 21:40:02 +02:00
void Player::pause()
2025-10-16 19:21:35 +02:00
{
2025-10-18 23:00:53 +02:00
qDebug() << "Player::pause(): Can't pause video?" << (!this->hasVideo() || this->loading());
if (!this->hasVideo() || this->loading())
2025-10-16 23:20:10 +02:00
{
return;
}
2025-10-18 21:40:02 +02:00
this->player.pause();
2025-10-16 19:21:35 +02:00
}
2025-10-18 21:40:02 +02:00
void Player::stop()
2025-10-16 19:21:35 +02:00
{
2025-10-18 23:00:53 +02:00
qDebug() << "Player::stop(): Can't stop video?" << (!this->hasVideo() || this->loading());
if (!this->hasVideo() || this->loading())
2025-10-16 23:20:10 +02:00
{
return;
}
this->player.stop();
2025-10-18 21:40:02 +02:00
this->unloadVideo();
}