We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

Commit 5d1df35b authored by elleo's avatar elleo

Add playlist parsing and playback to sailfish client

parent 7afd3238
...@@ -3,26 +3,89 @@ import QtMultimedia 5.0 ...@@ -3,26 +3,89 @@ import QtMultimedia 5.0
import Sailfish.Silica 1.0 import Sailfish.Silica 1.0
Page { Page {
id: radioPage;
property string station: ""; property string station: "";
property int currentTrack: 0;
Component.onCompleted: { Component.onCompleted: {
tune(station); tune(station);
} }
ListModel {
id: playlist;
}
MediaPlayer { MediaPlayer {
id: player id: player
autoPlay: true
onError: {
console.log("Track unplayable");
playNext();
}
onStatusChanged: {
if(status == MediaPlayer.EndOfMedia) {
playNext();
}
}
onSourceChanged: {
creator.text = playlist.get(currentTrack).creator;
title.text = playlist.get(currentTrack).title;
cover.source = playlist.get(currentTrack).image;
console.log("Source changed: " + source);
}
} }
Column { Column {
anchors.fill: parent anchors.fill: parent
Label { PageHeader {
id: stationName id: stationName
anchors.horizontalCenter: parent.horizontalCenter; anchors.horizontalCenter: parent.horizontalCenter;
} }
Image {
id: cover
width: parent.width;
height: parent.width;
}
Row {
width: parent.width
Label {
id: creator
}
Label {
text: " - "
}
Label {
id: title
}
}
SilicaListView {
model: playlist
visible: false;
height: parent.height / 2.0;
width: parent.width;
delegate: Component {
Row {
Label {
text: creator
}
Label {
text: " - "
}
Label {
text: title
}
}
}
}
} }
Row { Row {
...@@ -66,13 +129,21 @@ Page { ...@@ -66,13 +129,21 @@ Page {
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
Image { Image {
anchors.centerIn: parent anchors.centerIn: parent
source: player.playing ? "image://theme/icon-m-pause" source: player.playbackState == player.PlayingState ? "image://theme/icon-m-pause"
: "image://theme/icon-m-play" : "image://theme/icon-m-play"
opacity: play.pressed ? 0.4 : 1.0 opacity: play.pressed ? 0.4 : 1.0
} }
MouseArea { MouseArea {
id: play id: play
anchors.fill: parent anchors.fill: parent
onClicked: {
player.source = playlist.get(currentTrack).location;
if(player.playbackState == player.PlayingState) {
player.pause();
} else {
player.play();
}
}
} }
} }
...@@ -87,6 +158,9 @@ Page { ...@@ -87,6 +158,9 @@ Page {
MouseArea { MouseArea {
id: next id: next
anchors.fill: parent anchors.fill: parent
onClicked: {
playNext();
}
} }
} }
} }
...@@ -105,7 +179,7 @@ Page { ...@@ -105,7 +179,7 @@ Page {
var st = e.childNodes[i]; var st = e.childNodes[i];
for(var j = 0; j < st.childNodes.length; j++ ) { for(var j = 0; j < st.childNodes.length; j++ ) {
if(st.childNodes[j].nodeName === "name") { if(st.childNodes[j].nodeName === "name") {
stationName.text = st.childNodes[j].childNodes[0].nodeValue; stationName.title = st.childNodes[j].childNodes[0].nodeValue;
} }
} }
fetchPlaylist(); fetchPlaylist();
...@@ -116,12 +190,65 @@ Page { ...@@ -116,12 +190,65 @@ Page {
function fetchPlaylist() { function fetchPlaylist() {
request("method=radio.getplaylist&sk=" + wsKey, "post", function(doc) { request("method=radio.getplaylist&sk=" + wsKey, "post", function(doc) {
console.log(doc.responseText) ;
var e = doc.responseXML.documentElement; var e = doc.responseXML.documentElement;
for(var i = 0; i < e.childNodes.length; i++) { for(var i = 0; i < e.childNodes.length; i++) {
console.log(e.childNodes[i].nodeName); if(e.childNodes[i].nodeName === "error") {
showError(e.childNodes[i]);
}
if(e.childNodes[i].nodeName === "trackList") {
var tl = e.childNodes[i];
for(var j = 0; j < tl.childNodes.length; j++) {
if(tl.childNodes[j].nodeName === "track") {
var t = tl.childNodes[j];
var track = {}
for(var k = 0; k < t.childNodes.length; k++) {
try {
if(t.childNodes[k].nodeName === "location") {
track.location = t.childNodes[k].childNodes[0].nodeValue;
} else if(t.childNodes[k].nodeName === "title") {
track.title= t.childNodes[k].childNodes[0].nodeValue;
} else if(t.childNodes[k].nodeName === "album") {
track.album = t.childNodes[k].childNodes[0].nodeValue;
} else if(t.childNodes[k].nodeName === "creator") {
track.creator = t.childNodes[k].childNodes[0].nodeValue;
} else if(t.childNodes[k].nodeName === "duration") {
track.duration = t.childNodes[k].childNodes[0].nodeValue;
} else if(t.childNodes[k].nodeName === "image") {
track.image = t.childNodes[k].childNodes[0].nodeValue;
}
} catch(err) {
console.debug(err.message);
}
}
playlist.append(track)
}
}
}
} }
}); });
} }
function playPrev() {
currentTrack--;
if(currentTrack < 0) {
currentTrack = 0;
}
player.source = playlist.get(currentTrack).location;
}
function playNext() {
currentTrack++;
console.log(currentTrack + " / " + (playlist.count - 1));
if(currentTrack > playlist.count - 3) {
fetchPlaylist();
}
if(currentTrack > playlist.count) {
// We're having difficulty getting the next playlist set in TimePicker
// so play the last song in the playlist again
currentTrack--;
}
player.source = playlist.get(currentTrack).location;
}
} }
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