#include "player.h" // Public 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(); }); 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); this->player.setAudioOutput(&this->output); qDebug("Player::Player(): Constructed"); } 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(); } void Player::setVolume(float newVolume) { this->output.setVolume(newVolume); } // Public slots void Player::loadVideo(QString id) { if (this->hasVideo()) { 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())); emit this->hasVideoChanged(); emit this->loadingChanged(); 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; } void Player::unloadVideo() { qDebug("Player::unloadVideo(): Video unloaded"); 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() { qDebug() << "Player::play(): Can't play video?" << this->hasVideo() << this->loading(); if (!this->hasVideo() || this->loading()) { return; } this->player.play(); } void Player::pause() { qDebug() << "Player::pause(): Can't pause video?" << (!this->hasVideo() || this->loading()); if (!this->hasVideo() || this->loading()) { return; } this->player.pause(); } void Player::stop() { qDebug() << "Player::stop(): Can't stop video?" << (!this->hasVideo() || this->loading()); if (!this->hasVideo() || this->loading()) { return; } this->player.stop(); this->unloadVideo(); }