-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
154 lines (133 loc) · 3.13 KB
/
index.js
File metadata and controls
154 lines (133 loc) · 3.13 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
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
const settings = require('standard-settings').getSettings()
const assignment = require('assignment')
const Mpv = require('node-mpv')
const { SpacebroClient } = require('spacebro-client')
const path = require('path')
const version = require('./package').version
const client = new SpacebroClient(settings.service.spacebro)
const player = new Mpv({
verbose: settings.mpv.verbose,
socket: `/tmp/node-mpv-bro-${version}.sock`,
debug: settings.mpv.debug,
audio_only: false
}, settings.mpvParams)
const currentFilepath = ''
player.on('stopped', function () {
console.log('⏹ - stop', currentFilepath && path.basename(currentFilepath))
})
player.on('started', function () {
console.log('⏯ - start', currentFilepath && path.basename(currentFilepath))
})
player.on('paused', function () {
console.log('⏸ - pause', currentFilepath && path.basename(currentFilepath))
})
player.start()
.then(() => {
console.log('✅ player ready')
})
.catch((error) => {
console.error(error)
})
const mpvOptions = {
}
const mpvLoad = (filepath, options) => {
player.stop()
.then(() => {
return player.load(filepath)
})
.then(() => {
if (options && options.inf) {
return player.loop('inf')
} else {
return player.getDuration()
}
})
.then(() => {
return player.pause()
})
.catch((error) => {
console.error(error)
})
}
const mpvLoadAndPlay = (filepath, options) => {
player.stop()
.then(() => {
return player.load(filepath)
})
.then(() => {
if (options && options.inf) {
return player.loop('inf')
} else {
return player.play()
}
})
.catch((error) => {
console.error(error)
})
}
const mpvStop = () => {
player.stop()
.catch((error) => {
console.error(error)
})
}
const mpvPlay = () => {
player.play()
.catch((error) => {
console.error(error)
})
}
const mpvPause = () => {
player.pause()
.catch((error) => {
console.error(error)
})
}
const mpvResume = () => {
player.resume()
.catch((error) => {
console.error(error)
})
}
client.on('connect', (data) => {
console.log('connected to server')
})
client.on('loadAndPlay', (data) => {
data = assignment(JSON.parse(JSON.stringify(settings.media)), data)
console.log('loadAndplay received', data)
if (data.path) {
const options = data.options
mpvLoadAndPlay(data.path, options)
} else {
console.log('play event received with no path. Please provide a path to play')
}
})
client.on('loadPath', (data) => {
if (data.path) {
const options = data.options
mpvLoad(data.path, options)
} else {
console.log('load receive but no path set')
}
})
client.on('stop', (data) => {
console.log('stop - received')
mpvStop()
})
client.on('play', (data) => {
console.log('play - received')
mpvPlay()
})
client.on('pause', (data) => {
console.log('pause - received')
mpvPause()
})
client.on('resume', (data) => {
console.log('resume - received')
mpvResume()
})
process.on('uncaughtException', function (err) {
// handle the error safely
console.log(err)
player.quit()
})