Unity 8
DashAudioPlayer.qml
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  */
16 
17 pragma Singleton
18 import QtQuick 2.4
19 import QtMultimedia 5.6
20 import Dash 0.1
21 
22 QtObject {
23  id: root
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
31 
32  function playSource(newSource, newPlaylist) {
33  if (!priv.audio) {
34  console.info("DashAudioPlayer: creating player");
35  priv.audio = priv.audioComponent.createObject(root);
36  }
37  stop();
38  priv.audio.playlist.clear();
39  if (newPlaylist) {
40  // Look for newSource in newPlaylist
41  var sourceIndex = -1;
42  for (var i in newPlaylist) {
43  if (AudioUrlComparer.compare(newSource, newPlaylist[i])) {
44  sourceIndex = i;
45  break;
46  }
47  }
48  var urls = [];
49  if (sourceIndex === -1 && newSource != "") {
50  // If the playing song is not in the playlist, add it
51  urls.push(newSource);
52  sourceIndex = 0;
53  }
54  for (var i in newPlaylist) {
55  urls.push(newPlaylist[i]);
56  }
57  priv.audio.playlist.addItems(urls);
58  priv.audio.playlist.currentIndex = sourceIndex;
59  } else {
60  priv.audio.playlist.addItem(newSource);
61  priv.audio.playlist.currentIndex = 0;
62  }
63  play();
64  }
65 
66  function stop() {
67  if (priv.audio) {
68  priv.audio.stop();
69  }
70  }
71 
72  function play() {
73  if (priv.audio) {
74  priv.audio.play();
75  }
76  }
77 
78  function pause() {
79  if (priv.audio) {
80  priv.audio.pause();
81  }
82  }
83 
84  property QtObject priv: QtObject {
85  id: priv
86  property Audio audio: null
87  property Component audioComponent: Component {
88  Audio {
89  playlist: Playlist {
90  objectName: "playlist"
91  }
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).
96  */
97  onError: {
98  console.warn("DashAudioPlayer: error event (" +
99  priv.audio.errorString + "), destroying");
100  priv.audio.destroy();
101  }
102  }
103  }
104  }
105 
106  function lengthToString(s) {
107  if (typeof(s) !== "number" || s < 0) return "";
108 
109  var sec = "" + s % 60;
110  if (sec.length == 1) sec = "0" + sec;
111  var hour = Math.floor(s / 3600);
112  if (hour < 1) {
113  return Math.floor(s / 60) + ":" + sec;
114  } else {
115  var min = "" + Math.floor(s / 60) % 60;
116  if (min.length == 1) min = "0" + min;
117  return hour + ":" + min + ":" + sec;
118  }
119  }
120 }