Files
qyouvideo/player.cpp

182 lines
4.3 KiB
C++
Raw Permalink Normal View History

2025-10-16 19:21:35 +02:00
#include "player.h"
2025-10-19 02:58:12 +02:00
// HOLY GRAIL
2025-10-20 02:53:30 +02:00
// https://doc.qt.io/qt-6/qaudiobufferoutput.html
2025-10-19 02:58:12 +02:00
2025-10-16 23:20:10 +02:00
Player::Player(QObject *parent) : QObject(parent),
2025-10-20 02:53:30 +02:00
player(this), output(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] () {
2025-10-19 02:58:12 +02:00
const QMediaPlayer::MediaStatus status = this->player.mediaStatus();
if (status == QMediaPlayer::NoMedia)
{
qDebug("Player::Player(): NoMedia");
2025-10-20 02:53:30 +02:00
this->unloadVideo();
2025-10-19 02:58:12 +02:00
}
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
{
2025-10-20 02:53:30 +02:00
if (this->m_position == 0)
2025-10-19 02:58:12 +02:00
{
2025-10-20 02:53:30 +02:00
this->player.play();
2025-10-19 02:58:12 +02:00
}
2025-10-20 02:53:30 +02:00
this->m_loading = false;
emit this->loadingChanged();
2025-10-19 02:58:12 +02:00
}
});
QObject::connect(&this->player, &QMediaPlayer::positionChanged, this, [this] () {
this->m_position = this->player.position();
emit this->positionChanged();
});
2025-10-20 02:53:30 +02:00
// QObject::connect(&this->player, &QMediaPlayer::bufferProgressChanged, this, [this] () {
// this->m_buffered = this->player.bufferedTimeRange().latestTime() / this->m_duration;
// emit this->bufferedChanged();
// });
2025-10-19 02:58:12 +02:00
QObject::connect(&this->player, &QMediaPlayer::errorOccurred, this, [this] () {
this->m_failed = true;
qDebug("Player::Player() failedChanged");
emit this->failedChanged();
2025-10-18 21:40:02 +02:00
});
2025-10-16 23:20:10 +02:00
QObject::connect(&this->player, &QMediaPlayer::playbackStateChanged, this, [this] () {
2025-10-19 02:58:12 +02:00
qDebug("Player::Player() playingChanged");
2025-10-16 23:20:10 +02:00
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
2025-10-19 02:58:12 +02:00
// ********
// Read
// ********
2025-10-18 21:40:02 +02:00
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();
}
2025-10-19 02:58:12 +02:00
// *********
// Write
// *********
2025-10-16 23:20:10 +02:00
void Player::setVolume(float newVolume)
{
this->output.setVolume(newVolume);
}
2025-10-19 02:58:12 +02:00
void Player::setPosition(float newPosition)
{
this->player.setPosition(newPosition);
}
// ***********
// Methods
// ***********
2025-10-20 02:53:30 +02:00
// 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)
{
2025-10-20 02:53:30 +02:00
if (this->m_id != id)
2025-10-18 21:40:02 +02:00
{
this->unloadVideo();
}
2025-10-19 02:58:12 +02:00
this->m_id = id;
emit this->idChanged();
2025-10-20 02:53:30 +02:00
this->player.setSource(QUrl("https://youvideo.nonamesoft.xyz/youvideo/api/videofile/with_extension/" + id + extension));
2025-10-19 02:58:12 +02:00
2025-10-20 02:53:30 +02:00
this->m_title = title;
emit this->titleChanged();
2025-10-19 02:58:12 +02:00
2025-10-20 02:53:30 +02:00
this->m_position = position;
2025-10-19 02:58:12 +02:00
emit this->positionChanged();
this->m_buffered = 0;
emit this->bufferedChanged();
this->m_loading = true;
2025-10-18 23:00:53 +02:00
emit this->loadingChanged();
2025-10-19 02:58:12 +02:00
2025-10-18 21:40:02 +02:00
this->m_failed = false;
emit this->failedChanged();
2025-10-20 02:53:30 +02:00
qDebug() << "Player::loadVideo(): Loaded video https://youvideo.nonamesoft.xyz/youvideo/api/videofile/with_extension/" + id + extension;
}
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-20 02:53:30 +02:00
this->m_id = "";
emit this->idChanged();
2025-10-18 21:40:02 +02:00
this->player.setSource(QUrl(""));
this->player.stop();
2025-10-20 02:53:30 +02:00
this->m_title = "";
emit this->titleChanged();
this->m_loading = false;
emit this->loadingChanged();
2025-10-18 21:40:02 +02:00
this->m_failed = false;
2025-10-20 02:53:30 +02:00
emit this->failedChanged();
}
2025-10-18 21:40:02 +02:00
void Player::play()
{
2025-10-20 02:53:30 +02:00
if (this->m_id.length() == 0 || this->m_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-20 02:53:30 +02:00
if (this->m_id.length() == 0 || this->m_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
}