-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
61 lines (50 loc) · 1.27 KB
/
index.js
File metadata and controls
61 lines (50 loc) · 1.27 KB
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
var os = require('os');
var config = require('./config.json');
var username = config.spotify_username;
var password = config.spotify_password;
var server_address = config.server_address;
var spotify_init;
if (os.arch() == 'arm') {
spotify_init = require('./lib/pi/spotify');
} else {
spotify_init = require('./lib/mac/spotify');
}
var spotify = spotify_init({ appkeyFile: 'spotify_appkey.key' });
var queue = [];
var playing = false;
var playSongFromQueue = function() {
if (queue.length) {
spotify.player.play(queue.shift());
playing = true;
} else {
playing = false;
}
};
spotify.on({
ready: playSongFromQueue
});
spotify.player.on({
endOfTrack: playSongFromQueue
});
spotify.login(username, password, false, false);
var socket = require('socket.io-client')(server_address);
socket.on('add track to queue', function(link) {
var track = spotify.createFromLink(link);
queue.push(track);
if (!playing) playSongFromQueue();
});
socket.on('play', function() {
if (!playing) {
spotify.player.resume();
playing = true;
}
});
socket.on('pause', function() {
if (playing) {
spotify.player.pause();
playing = false;
}
});
socket.on('skip', function() {
playSongFromQueue();
});