~saviq/unity8/skip-logind-test

« back to all changes in this revision

Viewing changes to plugins/Dash/DashAudioPlayer.qml

  • Committer: CI Train Bot
  • Author(s): Albert Astals Cid, Michael Zanetti
  • Date: 2016-01-07 08:04:43 UTC
  • mfrom: (1757.3.53 audioCardSupport)
  • Revision ID: ci-train-bot@canonical.com-20160107080443-6khrb6nm9o075wl4
Introduce audio cards
Approved by: Michał Sawicz, Pawel Stolowski, Andrea Cimitan

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
    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
 
29
 
 
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
    }
 
59
 
 
60
    function stop() {
 
61
        audio.stop();
 
62
    }
 
63
 
 
64
    function play() {
 
65
        audio.play();
 
66
    }
 
67
 
 
68
    function pause() {
 
69
        audio.pause();
 
70
    }
 
71
 
 
72
    property QtObject audio: Audio {
 
73
        id: audio
 
74
        objectName: "audio"
 
75
        playlist: Playlist {
 
76
            objectName: "playlist"
 
77
        }
 
78
 
 
79
        onErrorStringChanged: console.warn("Dash Audio player error:", errorString)
 
80
    }
 
81
 
 
82
    function lengthToString(s) {
 
83
        if (typeof(s) !== "number" || s < 0) return "";
 
84
 
 
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
    }
 
96
}