Commit dc0bab55 authored by Mike Sheldon's avatar Mike Sheldon

Add playlist parsing and playback to sailfish client

parent 85f7c5ca
......@@ -3,26 +3,89 @@ import QtMultimedia 5.0
import Sailfish.Silica 1.0
Page {
id: radioPage;
property string station: "";
property int currentTrack: 0;
Component.onCompleted: {
tune(station);
}
ListModel {
id: playlist;
}
MediaPlayer {
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 {
anchors.fill: parent
Label {
PageHeader {
id: stationName
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 {
......@@ -66,13 +129,21 @@ Page {
anchors.verticalCenter: parent.verticalCenter
Image {
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"
opacity: play.pressed ? 0.4 : 1.0
}
MouseArea {
id: play
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 {
MouseArea {
id: next
anchors.fill: parent
onClicked: {
playNext();
}
}
}
}
......@@ -105,7 +179,7 @@ Page {
var st = e.childNodes[i];
for(var j = 0; j < st.childNodes.length; j++ ) {
if(st.childNodes[j].nodeName === "name") {
stationName.text = st.childNodes[j].childNodes[0].nodeValue;
stationName.title = st.childNodes[j].childNodes[0].nodeValue;
}
}
fetchPlaylist();
......@@ -116,12 +190,65 @@ Page {
function fetchPlaylist() {
request("method=radio.getplaylist&sk=" + wsKey, "post", function(doc) {
console.log(doc.responseText) ;
var e = doc.responseXML.documentElement;
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