~ahayzen/music-app/add-url-dispatcher-tests

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * Copyright (C) 2013, 2014
 *      Andrew Hayzen <ahayzen@gmail.com>
 *      Daniel Holm <d.holmen@gmail.com>
 *      Victor Thompson <victor.thompson@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import QtQuick 2.3
import QtMultimedia 5.0
import QtQuick.LocalStorage 2.0
import "settings.js" as Settings

/*
 * This file should *only* manage the media playing and the relevant settings
 * It should therefore only access MediaPlayer, trackQueue and Settings
 * Anything else within the app should use Connections to listen for changes
 */


Item {
    objectName: "player"

    property string currentMetaAlbum: ""
    property string currentMetaArt: ""
    property string currentMetaArtist: ""
    property string currentMetaFile: ""
    property string currentMetaTitle: ""
    property int currentIndex: -1
    property alias duration: mediaPlayer.duration
    property bool isPlaying: player.playbackState === MediaPlayer.PlayingState
    property alias playbackState: mediaPlayer.playbackState
    property alias position: mediaPlayer.position
    property bool repeat: Settings.getSetting("repeat") === "1"
    property bool shuffle: Settings.getSetting("shuffle") === "1"
    property alias source: mediaPlayer.source
    property alias volume: mediaPlayer.volume

    signal stopped()

    onRepeatChanged: {
        Settings.setSetting("repeat", repeat ? "1" : "0")
        console.debug("Repeat:", Settings.getSetting("repeat") === "1")
    }

    onShuffleChanged: {
        Settings.setSetting("shuffle", shuffle ? "1" : "0")
        console.debug("Shuffle:", Settings.getSetting("shuffle") === "1")
    }

    Connections {
        target: trackQueue.model
        onCountChanged: {
            if (trackQueue.model.count === 1) {
                player.currentIndex = 0;
                player.source = Qt.resolvedUrl(trackQueue.model.get(0).filename)
            } else if (trackQueue.model.count === 0) {
                player.currentMetaFile = ""
                player.source = ""
            }
        }
    }

    function getSong(direction, startPlaying, fromControls) {
        // Seek to start if threshold reached when selecting previous
        if (direction === -1 && (player.position / 1000) > 5)
        {
            player.seek(0);  // seek to start
            return;
        }

        if (trackQueue.model.count == 0)
        {
            customdebug("No tracks in queue.");
            return;
        }

        // default fromControls and startPlaying to true
        fromControls = fromControls === undefined ? true : fromControls;
        startPlaying = startPlaying === undefined ? true : startPlaying;
        var newIndex;

        console.log("currentIndex: " + currentIndex)
        console.log("trackQueue.count: " + trackQueue.model.count)

        // Do not shuffle if repeat is off and there is only one track in the queue
        if (shuffle && !(trackQueue.model.count === 1 && !repeat)) {
            var now = new Date();
            var seed = now.getSeconds();

            // trackQueue must be above 1 otherwise an infinite loop will occur
            do {
                newIndex = (Math.floor((trackQueue.model.count)
                                       * Math.random(seed)));
            } while (newIndex === currentIndex && trackQueue.model.count > 1)
        } else {
            if ((currentIndex < trackQueue.model.count - 1 && direction === 1 )
                    || (currentIndex > 0 && direction === -1)) {
                newIndex = currentIndex + direction
            } else if(direction === 1 && (repeat || fromControls)) {
                newIndex = 0
            } else if(direction === -1 && (repeat || fromControls)) {
                newIndex = trackQueue.model.count - 1
            }
            else
            {
                player.stop()
                return;
            }
        }

        if (startPlaying) {  // only start the track if told
            playSong(trackQueue.model.get(newIndex).filename, newIndex)
        }
        else {
            currentIndex = newIndex
            source = Qt.resolvedUrl(trackQueue.model.get(newIndex).filename)
        }
    }

    function nextSong(startPlaying, fromControls) {
        getSong(1, startPlaying, fromControls)
    }

    function pause() {
        mediaPlayer.pause();
    }

    function play() {
        mediaPlayer.play();
    }

    function playSong(filepath, index) {
        player.stop();
        currentIndex = index;
        player.source = Qt.resolvedUrl(filepath);
        player.play();
    }

    function previousSong(startPlaying) {
        getSong(-1, startPlaying)
    }

    function seek(position) {
        mediaPlayer.seek(position);
    }

    function stop() {
        mediaPlayer.stop();
    }

    function toggle() {
        if (player.playbackState == MediaPlayer.PlayingState) {
            player.pause()
        }
        else {
            player.play()
        }
    }

    MediaPlayer {
        id: mediaPlayer
        muted: false

        onSourceChanged: {
            // Force invalid source to ""
            if (source === undefined || source === false) {
                source = ""
                return
            }

            if (source === "") {
                currentIndex = -1
                player.stop()
            }
            else {
                var obj = trackQueue.model.get(player.currentIndex);
                currentMetaAlbum = obj.album;
                currentMetaArt = obj.art;
                currentMetaArtist = obj.author;
                currentMetaFile = obj.filename;
                currentMetaTitle = obj.title;
            }

            console.log("Source: " + source.toString())
            console.log("Index: " + currentIndex)
        }

        onStatusChanged: {
            if (status == MediaPlayer.EndOfMedia) {
                nextSong(true, false) // next track
            }
        }

        onStopped: player.stopped()
    }
}