2 * Copyright (C) 2015 Canonical, Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 import QtMultimedia 5.6
24 readonly property real progress: priv.audio ? priv.audio.position / priv.audio.duration : 0.0
25 readonly property bool playing: priv.audio ? priv.audio.playbackState === Audio.PlayingState : false
26 readonly property bool paused: priv.audio ? priv.audio.playbackState === Audio.PausedState : false
27 readonly property bool stopped: priv.audio ? priv.audio.playbackState === Audio.StoppedState : true
28 readonly property int position: priv.audio ? priv.audio.position : 0
29 readonly property url currentSource: priv.audio ? priv.audio.playlist.currentItemSource : ""
30 readonly property Playlist playlist: priv.audio ? priv.audio.playlist : null
32 function playSource(newSource, newPlaylist) {
34 console.info("DashAudioPlayer: creating player");
35 priv.audio = priv.audioComponent.createObject(root);
38 priv.audio.playlist.clear();
40 // Look for newSource in newPlaylist
42 for (var i in newPlaylist) {
43 if (AudioUrlComparer.compare(newSource, newPlaylist[i])) {
49 if (sourceIndex === -1 && newSource != "") {
50 // If the playing song is not in the playlist, add it
54 for (var i in newPlaylist) {
55 urls.push(newPlaylist[i]);
57 priv.audio.playlist.addItems(urls);
58 priv.audio.playlist.currentIndex = sourceIndex;
60 priv.audio.playlist.addItem(newSource);
61 priv.audio.playlist.currentIndex = 0;
84 property QtObject priv: QtObject {
86 property Audio audio: null
87 property Component audioComponent: Component {
90 objectName: "playlist"
92 /* Remove player in case of error so it gets recreated next time
93 * we need it. Happens if backend media player restarted, for
94 * instance. qtmultimedia should probably handle this
95 * transparently (LP: #1616425).
98 console.warn("DashAudioPlayer: error event (" +
99 priv.audio.errorString + "), destroying");
100 priv.audio.destroy();
106 function lengthToString(s) {
107 if (typeof(s) !== "number" || s < 0) return "";
109 var sec = "" + s % 60;
110 if (sec.length == 1) sec = "0" + sec;
111 var hour = Math.floor(s / 3600);
113 return Math.floor(s / 60) + ":" + sec;
115 var min = "" + Math.floor(s / 60) % 60;
116 if (min.length == 1) min = "0" + min;
117 return hour + ":" + min + ":" + sec;