Reimplement streaming
This commit is contained in:
6
.vscode/settings.json
vendored
6
.vscode/settings.json
vendored
@@ -1,6 +1,10 @@
|
|||||||
{
|
{
|
||||||
"files.associations": {
|
"files.associations": {
|
||||||
"flake.lock": "json",
|
"flake.lock": "json",
|
||||||
"qobject": "cpp"
|
"qobject": "cpp",
|
||||||
|
"chrono": "cpp",
|
||||||
|
"variant": "cpp",
|
||||||
|
"qtmultimedia": "cpp",
|
||||||
|
"qdebug": "cpp"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
8
Main.qml
8
Main.qml
@@ -121,14 +121,6 @@ ApplicationWindow {
|
|||||||
text: qsTr("Settings")
|
text: qsTr("Settings")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Button {
|
|
||||||
// text: "S"
|
|
||||||
// onClicked: function() {
|
|
||||||
// var component = Qt.createComponent("ViewSettings.qml")
|
|
||||||
// var window = component.createObject(root)
|
|
||||||
// window.show()
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
59
player.cpp
59
player.cpp
@@ -1,7 +1,23 @@
|
|||||||
#include "player.h"
|
#include "player.h"
|
||||||
|
|
||||||
Player::Player(QObject *parent) : QObject(parent)
|
// Public
|
||||||
|
Player::Player(QObject *parent) : QObject(parent),
|
||||||
|
player(this),
|
||||||
|
output(this)
|
||||||
{
|
{
|
||||||
|
QObject::connect(&this->player, &QMediaPlayer::playbackStateChanged, this, [this] () {
|
||||||
|
qDebug("Player::Player()->Lambda: Playing got changed!");
|
||||||
|
emit this->playingChanged();
|
||||||
|
});
|
||||||
|
|
||||||
|
QObject::connect(&this->output, &QAudioOutput::volumeChanged, this, [this] () {
|
||||||
|
qDebug("Player::Player()->Lambda: Volume got changed!");
|
||||||
|
emit this->volumeChanged();
|
||||||
|
});
|
||||||
|
|
||||||
|
this->output.setVolume(0.3);
|
||||||
|
this->player.setAudioOutput(&this->output);
|
||||||
|
|
||||||
qDebug("Player::Player(): Constructed");
|
qDebug("Player::Player(): Constructed");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -10,12 +26,47 @@ Player::~Player()
|
|||||||
qDebug("Player::~Player(): Destructed");
|
qDebug("Player::~Player(): Destructed");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::startPlaying(int index)
|
bool Player::playing() const
|
||||||
{
|
{
|
||||||
qDebug("Player::startPlaying(): Success, I can call between both environments!");
|
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::startPlaying(u_int8_t index)
|
||||||
|
{
|
||||||
|
if (index < 0 || index >= maxStations)
|
||||||
|
{
|
||||||
|
qWarning("Player::startPlaying(): Tried accessing out of bounds.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (this->playing()) {
|
||||||
|
this->stopPlaying();
|
||||||
|
}
|
||||||
|
this->player.setSource(QUrl(streamPrefix + stations[index].slug));
|
||||||
|
this->player.play();
|
||||||
|
qDebug("Player::startPlaying(): Starting playing");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::stopPlaying()
|
void Player::stopPlaying()
|
||||||
{
|
{
|
||||||
qDebug("Player::stopPlaying(): Success, I can even tell C++ to stop!");
|
if (this->playing())
|
||||||
|
{
|
||||||
|
qWarning("Player::stopPlaying(): Prevented redundant stopPlaying() call.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this->player.stop();
|
||||||
|
this->player.setSource(QUrl(""));
|
||||||
|
qDebug("Player::stopPlaying(): Stopping playback...");
|
||||||
}
|
}
|
||||||
41
player.h
41
player.h
@@ -1,18 +1,55 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <QtMultimedia>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QtQmlIntegration/qqmlintegration.h>
|
#include <QtQmlIntegration/qqmlintegration.h>
|
||||||
|
|
||||||
|
constexpr std::string_view metadataEndpoint = "https://youradio.nonamesoft.xyz/youradio/api/status-json.xsl";
|
||||||
|
constexpr std::string_view streamPrefix = "https://youradio.nonamesoft.xyz/youradio/api/";
|
||||||
|
|
||||||
|
struct Station
|
||||||
|
{
|
||||||
|
const char name[16];
|
||||||
|
const char slug[16];
|
||||||
|
QString* songTitle;
|
||||||
|
u_int8_t listeners;
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr u_int8_t maxStations = 3;
|
||||||
|
|
||||||
|
const Station stations[maxStations] = {
|
||||||
|
{ "Autoradio", "autoradio" , nullptr, 0 },
|
||||||
|
{ "Live Mix", "livemix" , nullptr, 0 },
|
||||||
|
{ "Deep Bass", "bassboosted", nullptr, 0 }
|
||||||
|
};
|
||||||
|
|
||||||
class Player : public QObject
|
class Player : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
QML_SINGLETON
|
QML_SINGLETON
|
||||||
QML_ELEMENT
|
QML_ELEMENT
|
||||||
|
|
||||||
|
Q_PROPERTY(bool playing READ playing NOTIFY playingChanged)
|
||||||
|
Q_PROPERTY(float volume READ volume WRITE setVolume NOTIFY volumeChanged)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Player(QObject *parent = nullptr);
|
Player(QObject *parent = nullptr);
|
||||||
~Player();
|
~Player();
|
||||||
|
|
||||||
Q_INVOKABLE void startPlaying(int index);
|
bool playing() const;
|
||||||
Q_INVOKABLE void stopPlaying();
|
float volume() const;
|
||||||
|
void setVolume(float newVolume);
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void startPlaying(u_int8_t index);
|
||||||
|
void stopPlaying();
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void playingChanged();
|
||||||
|
void volumeChanged();
|
||||||
|
|
||||||
|
private:
|
||||||
|
QMediaPlayer player;
|
||||||
|
QAudioOutput output;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user