-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (29 loc) · 787 Bytes
/
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
'use strict'
const AirPlay = require('airplay-protocol')
const ADDRESS = 'Apple-TV-3rd-floor.local'
const play = (audioUrl, cb) => {
const device = new AirPlay(ADDRESS)
const getInfo = () => {
device.playbackInfo((err, res, playbackInfo) => {
setTimeout(getInfo, 2000)
if (err) return device.emit('error', err)
device.emit('playbackInfo', playbackInfo)
})
}
device.play(audioUrl, (err) => {
if (err) return cb(err)
const waitForPlay = () => {
device.playbackInfo((err, res, playbackInfo) => {
if (err) return cb(err)
device.emit('playbackInfo', playbackInfo)
if (playbackInfo && playbackInfo.readyToPlay) {
getInfo()
cb()
} else setTimeout(waitForPlay, 500)
})
}
waitForPlay()
})
return device
}
module.exports = play