-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
119 lines (102 loc) · 3.63 KB
/
index.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
var spawn = require('child_process').spawn;
var events = require('events');
var util = require('util');
var path = require('path');
var Playback = function() {
var that = this;
events.EventEmitter.call(this);
this.isWindows = !!process.platform.match(/^win/);
this.runTransportScript = function(command, callback) {
var scriptPath = this.isWindows ?
path.join(__dirname, 'windows_scripts', 'iTunes.js') :
path.join(__dirname, 'applescripts', 'ITunesTransport.scpt');
var scriptRunner = this.isWindows ?
spawn('cscript', ['//Nologo', scriptPath, command]) :
spawn('osascript', [scriptPath, command]);
scriptRunner.stdout.on('data', function (data) {
var result;
try {
result = JSON.parse(data);
} catch(e) {
result = data;
}
if (command === 'play') {
that.playing = result;
}
if (callback) {
callback(result);
} else {
if (command === 'play') {
that.playing = result;
that.emit('playing', result);
} else if (command === 'stop') {
that.emit('stopped', result);
} else if (command === 'pause') {
that.emit('paused', result);
} else if (command === 'currenttrack') {
that.emit('playing', result);
}
}
});
};
that.playing = null;
// Poll for changes to the current track
setInterval(function() {
that.runTransportScript('currenttrack', function(data) {
if (data || that.playing) {
var track;
try {
track = JSON.parse(data);
} catch(e) {
track = data;
}
if (that.playing && track) {
if (track.artist !== that.playing.artist ||
track.album !== that.playing.album ||
track.name !== that.playing.name ) {
that.playing = track;
that.emit('playing', track);
}
} else if (that.playing !== track) {
that.playing = track;
if (track) {
that.emit('playing', track);
} else {
that.emit('paused', track);
}
}
} else {
that.playing = null;
}
});
}, 200 );
};
util.inherits(Playback, events.EventEmitter);
Playback.prototype.play = function(callback) {
this.runTransportScript('play', callback);
};
Playback.prototype.pause = function(callback) {
this.runTransportScript('pause', callback);
};
Playback.prototype.stop = function(callback) {
this.runTransportScript('stop', callback);
};
Playback.prototype.currentTrack = function(callback) {
this.runTransportScript('currenttrack', callback);
};
Playback.prototype.next = function(callback) {
this.runTransportScript('next', callback);
};
Playback.prototype.previous = function(callback) {
this.runTransportScript('previous', callback);
};
Playback.prototype.fadeOut = function(callback) {
this.runTransportScript('fadeout', callback);
};
Playback.prototype.fadeIn = function(callback) {
this.runTransportScript('fadein', callback);
};
Playback.prototype.setVolume = function(volume, callback) {
this.runTransportScript('setvolume ' + volume, callback);
};
module.exports = new Playback();