on 4e3068c help sign in
unity8/plugins/Dash/DashAudioPlayer.qml
96 lines 3.0 kB history raw
1/*
2 * Copyright (C) 2015 Canonical, Ltd.
3 *
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.
7 *
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.
12 *
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/>.
15 */
17pragma Singleton
18import QtQuick 2.4
19import QtMultimedia 5.6
20import Dash 0.1
22QtObject {
23 readonly property real progress: audio.position / audio.duration
24 readonly property bool playing: audio.playbackState === Audio.PlayingState
25 readonly property bool paused: audio.playbackState === Audio.PausedState
26 readonly property bool stopped: audio.playbackState === Audio.StoppedState
27 readonly property alias position: audio.position
28 readonly property url currentSource: audio.playlist.currentItemSource
30 function playSource(newSource, newPlaylist) {
31 stop();
32 audio.playlist.clear();
33 if (newPlaylist) {
34 // Look for newSource in newPlaylist
35 var sourceIndex = -1;
36 for (var i in newPlaylist) {
37 if (AudioUrlComparer.compare(newSource, newPlaylist[i])) {
38 sourceIndex = i;
39 break;
40 }
41 }
42 var urls = [];
43 if (sourceIndex === -1 && newSource != "") {
44 // If the playing song is not in the playlist, add it
45 urls.push(newSource);
46 sourceIndex = 0;
47 }
48 for (var i in newPlaylist) {
49 urls.push(newPlaylist[i]);
50 }
51 audio.playlist.addItems(urls);
52 audio.playlist.currentIndex = sourceIndex;
53 } else {
54 audio.playlist.addItem(newSource);
55 audio.playlist.currentIndex = 0;
56 }
57 play();
58 }
60 function stop() {
61 audio.stop();
62 }
64 function play() {
65 audio.play();
66 }
68 function pause() {
69 audio.pause();
70 }
72 property QtObject audio: Audio {
73 id: audio
74 objectName: "audio"
75 playlist: Playlist {
76 objectName: "playlist"
77 }
79 onErrorStringChanged: console.warn("Dash Audio player error:", errorString)
80 }
82 function lengthToString(s) {
83 if (typeof(s) !== "number" || s < 0) return "";
85 var sec = "" + s % 60;
86 if (sec.length == 1) sec = "0" + sec;
87 var hour = Math.floor(s / 3600);
88 if (hour < 1) {
89 return Math.floor(s / 60) + ":" + sec;
90 } else {
91 var min = "" + Math.floor(s / 60) % 60;
92 if (min.length == 1) min = "0" + min;
93 return hour + ":" + min + ":" + sec;
94 }
95 }