-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmusicPlayer.js
141 lines (101 loc) · 2.64 KB
/
musicPlayer.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
/**
*
* JukeBox2 - musicPlayer.js
*
* License: see included copyright.txt
* User: leon
* Date: 6/1/13
* Time: 2:19 PM
*
*/
'use strict';
var config = require('./config').config;
var async = require('async');
var redis = require('redis');
var mplayer = require('simple-mplayer');
var events = require('events');
var MODE_WAITING = 'waiting';
var MODE_PLAYING = 'playing';
var MODE_PAUSED = 'paused';
var mode = MODE_WAITING;
var music;
var redisClient = redis.createClient(
config.REDIS_PORT,
config.REDIS_SERVER,
config.REDIS_OPTIONS
);
if (config.REDIS_AUTH) redisClient.auth(config.REDIS_AUTH, function () {
console.log('processed Redis auth');
});
function newSong(song) {
if ( ! song) {
console.error('Error: newSong() called with no song!');
return;
}
console.log('newSong(): ', song);
var musicFile = song.complete_name;
if ( ! musicFile) {
console.error('Error: newSong() called with a song with no complete_name!');
}
music = new mplayer(musicFile);
mode = MODE_PLAYING;
music.once('complete', function () {
music = undefined;
songDone(song);
});
music.play();
go();
}
function songDone(song) {
console.log('song done');
mode = MODE_WAITING;
go();
}
function checkQueue() {
redisClient.lpop(config.MUSIC_QUEUE, function (err, songString) {
var song = null;
if (err) {
console.error('Can NOT talk to REDIS!');
} else {
if (songString) {
song = JSON.parse(songString);
if (song) newSong(song);
}
}
if ( ! song) setTimeout(go, 1000);
});
}
function go() {
console.log('go()', new Date(), mode);
switch (mode) {
case MODE_WAITING:
checkQueue();
break;
case MODE_PLAYING:
break;
case MODE_PAUSED:
break;
default:
console.error('Internal Error: mode "' + mode + '" is not recognized!');
break;
}
}
go();
/*
// with ability to pause/resume/stop:
var music = new mplayer(musicFile);
music.once('complete', function (music) {
console.log('got "complete"');
});
//music.play({loop: 0}); // send "-loop 0" to MPlayer to loop the soundtrack forever
music.play();
setTimeout(function () {
music.pause(); // pause the music after one seconds
}, 3000);
setTimeout(function () {
music.resume(); // and resume it two seconds after pausing
}, 5000);
setTimeout(function () {
music.stop(); // and stop definitely seven seconds after resuming
}, 10000);
*/