Commit 845068db authored by Kevin Whitaker's avatar Kevin Whitaker

Use struct for track info to allow better track data management. Use reported...

Use struct for track info to allow better track data management. Use reported track duration instead of qt mediaplayer guessing.
parent de831a0c
......@@ -45,7 +45,7 @@ bool MiniMediaPlayer::nextTrack()
m_player->pause();
m_player->setPosition(0);
QtConcurrent::run([this]() {
m_player->setMedia(getStreamUrlFromUrl(playlist.first().second));
m_player->setMedia(getStreamUrlFromUrl(playlist.first().trackURL));
emit playlistChanged(getTrackNames());
});
return true;
......@@ -66,7 +66,7 @@ qint64 MiniMediaPlayer::getCurrentPosition()
qint64 MiniMediaPlayer::getTrackDuration()
{
return m_player->duration();
return playlist.first().trackLength;
}
int MiniMediaPlayer::getVolume()
......@@ -97,9 +97,9 @@ bool MiniMediaPlayer::playPauseTrack(bool playing)
QList<QString> MiniMediaPlayer::getTrackNames()
{
QList<QString> playlistTitles;
for(QPair<QString,QUrl> item: playlist)
for(trackInfo item: playlist)
{
playlistTitles.append(item.first);
playlistTitles.append(item.trackName);
}
return playlistTitles;
}
......@@ -107,7 +107,7 @@ QList<QString> MiniMediaPlayer::getTrackNames()
QMap<QString, qint64> MiniMediaPlayer::currentState()
{
QMap<QString,qint64> state;
state.insert("duration",m_player->duration());
state.insert("duration",playlist.first().trackLength);
state.insert("position",m_player->position());
state.insert("state",isPlaying());
return state;
......@@ -115,7 +115,7 @@ QMap<QString, qint64> MiniMediaPlayer::currentState()
void MiniMediaPlayer::durationChanged(qint64 length)
{
emit trackDurationChanged(length);
//emit trackDurationChanged(length); //Don't emit duration changed since we already have duration. TODO: might want to use this still if implement another media source besides youtube-dl.
}
void MiniMediaPlayer::positionChanged(qint64 position)
......@@ -130,7 +130,7 @@ void MiniMediaPlayer::addURLToPlaylist(QUrl mediaUrl)
{
bool shouldStartPlaying = false;
if(playlist.size() == 0) shouldStartPlaying = true;
QList<QPair<QString,QUrl>> tracks = getStreamTitlesAndUrlsFromUrl(mediaUrl);
QList<trackInfo> tracks = getStreamTitlesAndUrlsFromUrl(mediaUrl);
if(tracks.size() > 0)
{
playlist.append(tracks);
......@@ -138,7 +138,7 @@ void MiniMediaPlayer::addURLToPlaylist(QUrl mediaUrl)
}
if(shouldStartPlaying && playlist.size() > 0)
{
m_player->setMedia(getStreamUrlFromUrl(playlist.first().second));
m_player->setMedia(getStreamUrlFromUrl(playlist.first().trackURL));
}
}
});
......@@ -205,9 +205,9 @@ void MiniMediaPlayer::mediaStatusChanged(QMediaPlayer::MediaStatus status)
}
}
QList<QPair<QString,QUrl>> MiniMediaPlayer::getStreamTitlesAndUrlsFromUrl(QUrl mediaUrl)
QList<MiniMediaPlayer::trackInfo> MiniMediaPlayer::getStreamTitlesAndUrlsFromUrl(QUrl mediaUrl)
{
QList<QPair<QString,QUrl>> tracks;
QList<trackInfo> tracks;
//Run youtube-dl on Url and return json given back.
QProcess youtubedl;
youtubedl.start("youtube-dl --no-warnings -f bestaudio -j "+mediaUrl.toString());
......@@ -224,10 +224,11 @@ QList<QPair<QString,QUrl>> MiniMediaPlayer::getStreamTitlesAndUrlsFromUrl(QUrl m
QJsonDocument info = QJsonDocument::fromJson(jsonTrack.toUtf8());
if(!info.isNull() && info.isObject())
{
QPair<QString,QUrl> track;
trackInfo track;
QJsonObject jsonTrackInfo = info.object();
track.first = jsonTrackInfo["title"].toString();
track.second = QUrl(jsonTrackInfo["webpage_url"].toString());
track.trackName = jsonTrackInfo["title"].toString();
track.trackURL = QUrl(jsonTrackInfo["webpage_url"].toString());
track.trackLength = jsonTrackInfo["duration"].toInt() * 1000; //convert seconds to milliseconds
tracks.append(track);
}
}
......
......@@ -51,6 +51,12 @@ public:
void setVolume(int volume);
struct trackInfo {
QString trackName;
QUrl trackURL;
qint64 trackLength;
};
signals:
void playStateChanged(bool isPlaying);
void playlistChanged(QStringList trackTitles);
......@@ -66,11 +72,11 @@ private slots:
private:
QMediaPlayer *m_player;
QList<QPair<QString,QUrl>> playlist;
QList<trackInfo> playlist;
bool isValidMediaUrl(QUrl mediaUrl);
QUrl getStreamUrlFromUrl(QUrl mediaUrl);
QList<QPair<QString,QUrl>> getStreamTitlesAndUrlsFromUrl(QUrl mediaUrl);
QList<trackInfo> getStreamTitlesAndUrlsFromUrl(QUrl mediaUrl);
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment