-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.js
226 lines (199 loc) · 5.75 KB
/
Player.js
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
'use strict';
/**
* Created by Anwender on 07.04.2017.
*/
//var audioPlayer = require("play-sound")({"player": "./mpg123/mpg123.exe"});
if (process.platform === 'win32') {
var audioPlayer = require("play-sound")({"player": "./mplayer/mplayer.exe"});
} else {
var audioPlayer = require("play-sound")({"player": "mplayer"});
}
var songQueue = [];
var playing = false;
var player = null;
var paused = false;
// queueID ist forlaufende Nummer um Songs innerhalb der Queue eindeutig zu identifizieren
var queueID = 1;
var currentSongTimeout;
var killFlag = false;
var nextSongCallback = function (song) {
};
var queueChangeCallback = function (queue) {
};
var getQueue = function () {
// Aktuell spielender Song wird an erster Stelle des zurueckgebenen Arrays angefuegt
return songQueue;
};
var removeSongFromQueue = function (id) {
if (songQueue.length > 0 && songQueue[0].queueID == id) {
stop();
return true;
}
var idx = getIndexFromQueueID(id);
if (idx >= 0) {
songQueue.splice(idx, 1);
queueChangeCallback(getQueue());
return true;
}
return false;
};
var changeQueuePosition = function (oldQueueID, newQueueID) {
var idxOld = getIndexFromQueueID(oldQueueID);
console.log("idxOld: " + idxOld);
var songToMove;
if ((getIndexFromQueueID(newQueueID) === -1 && newQueueID !== 0) || idxOld === -1) {
//ziel oder quelle exisitiert nicht
return false;
} else if ((oldQueueID === newQueueID) || (idxOld === 0 && newQueueID === 0)) {
// old und new sind gleiche Position in queue
return true;
}
// zuverschiebender Song war in songQueue
songToMove = songQueue[idxOld];
songQueue.splice(idxOld, 1);
if (newQueueID === 0) {
// song als nowPlaying einfuegen
songQueue.splice(1, 0, songToMove,songQueue[0]);
} else {
//Song in die queue einfuegen
var idxNew = getIndexFromQueueID(newQueueID);
console.log("idxNew: " + idxNew);
songQueue.splice(idxNew + 1, 0, songToMove);
}
if (getIndexFromQueueID(oldQueueID) === 0 || newQueueID === 0) {
//durch stop wird queueChange callback aufgerufen
stop();
} else {
// sonst selbst callback aufrufen
queueChangeCallback(getQueue());
}
return true;
};
var getIndexFromQueueID = function (qID) {
for (var i = 0; i < songQueue.length; i++) {
if (songQueue[i].queueID === qID) {
return i;
}
}
return -1;
};
var addSong = function (song) {
song.queueID = queueID;
queueID++;
songQueue.push(song);
queueChangeCallback(getQueue());
// paused = false;
tick();
};
/**
*
* @param callback Funktion wird aufgerufen wenn die Wiedergabe startet
*/
var setNextSongCallback = function (callback) {
nextSongCallback = callback;
};
var pause = function () {
if (player != null) {
stop();
player = null;
}
paused = true;
};
var resume = function () {
paused = false;
tick();
};
var getCurrentSong = function () {
return songQueue[0];
};
/**
* Prueft ob ob aktuell ein Song laeuft und ob startet ggf. den naechsten Song
*/
var tick = function () {
if (player == null && !paused) {
//var naechsterSong = songQueue.shift();
if (songQueue.length > 0) {
// songQueue.splice(0,0,naechsterSong);
play(songQueue[0]);
}
queueChangeCallback(getQueue());
nextSongCallback(songQueue[0]);
} else if (player == null) {
//currentSong = null;
}
};
var play = function (song) {
if (player != null) {
stop();
}
// Hier Moeglichkeiten fuer andere datenquellen
if (song.source.type === "local") {
console.log('Play Song ' + song.id + ': ' + song.title);
player = audioPlayer.play(song.source.path, {"./mplayer/mplayer.exe": ['-really-quiet']}, function (err) {
if (err) {
console.log("Player Error: " + err);
//player.kill();
//player = null;
//tick();
} else {
console.log("song fertig");
}
if (currentSongTimeout != null) {
clearTimeout(currentSongTimeout);
}
if (player != null) {
console.log("kill " + songQueue[0].title);
player.kill();
player = null;
}
//currentSong = null;
songQueue.shift();
tick();
});
}
//currentSong = song;
songQueue[0].endTime = new Date().getTime() + (song.duration ) * 1000;
// currentSongTimeout = setTimeout(function () {
// if (currentSongTimeout != null) {
// clearTimeout(currentSongTimeout);
// }
// if (player != null) {
// console.log("kill " + currentSong.title);
// player.kill();
// player = null;
//
// }
// currentSong = null;
// tick();
// }, (song.duration + 1) * 1000);
};
/**
*
* @param callback wird aufgerufen wenn sich die songQueue veraendert
*/
var setQueueChangeCallback = function (callback) {
queueChangeCallback = callback;
};
/**
* Stopt die aktuelle Wiedergabe
*/
var stop = function () {
if (player != null) {
console.log("kill " + songQueue[0].title);
player.kill();
player = null;
}
//currentSong = null;
//tick();
};
module.exports = {
"addSong": addSong,
"setNextSongCallback": setNextSongCallback,
"pause": pause,
"resume": resume,
"getCurrentSong": getCurrentSong,
"getQueue": getQueue,
"removeSongFromQueue": removeSongFromQueue,
"setQueueChangeCallback": setQueueChangeCallback,
"changeQueuePosition": changeQueuePosition
};