-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpotifyReceiver.cpp
42 lines (35 loc) · 1.54 KB
/
SpotifyReceiver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "SpotifyReceiver.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QDebug>
SpotifyReceiver::SpotifyReceiver(QObject *parent) : QObject(parent)
{
udpSocket.bind(QHostAddress::Any, 12356); // Adjust the port as needed
connect(&udpSocket, &QUdpSocket::readyRead, this, &SpotifyReceiver::processPendingDatagrams);
}
void SpotifyReceiver::processPendingDatagrams() {
while (udpSocket.hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(udpSocket.pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
udpSocket.readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
// Decode the received data as a JSON object
QJsonParseError jsonError;
QJsonDocument jsonDoc = QJsonDocument::fromJson(datagram, &jsonError);
if (jsonError.error != QJsonParseError::NoError) {
qDebug() << "Error decoding JSON data:" << jsonError.errorString();
continue;
}
if (jsonDoc.isObject()) {
QJsonObject jsonObj = jsonDoc.object();
QString Track_Name=jsonObj["trackName"].toString();
QString Artist_Name=jsonObj["artistName"].toString();
QString Album_Name=jsonObj["albumName"].toString();
QString Album_Img_URL=jsonObj["albumURL"].toString();
bool isPlaying=jsonObj["isPlaying"].toBool();
emit spotifyReceivedData(Track_Name, Artist_Name, Album_Name, Album_Img_URL,isPlaying);
// qDebug()<<Artist_Name;
}
}
}