-
Notifications
You must be signed in to change notification settings - Fork 3
/
musicplayer.js
133 lines (116 loc) · 3.32 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
'use strict'
var config = require('./config')
var Logger = require('./logger')
var Mopidy = require("mopidy");
class MusicPlayer {
constructor() {
this.mopidy = new Mopidy({
webSocketUrl: config.mopidyAddress,
callingConvention: "by-position-or-by-name"
})
this.playing = false
this.volume = 100
setInterval(()=>this.getState().then(), 10000)
}
playPodcast(uri) {
var mopidy = this.mopidy
this.playing = true
mopidy.mixer.setVolume({volume: 100})
mopidy.tracklist.clear().then(()=>{
mopidy.library.browse({uri: uri }).then((result)=>{
mopidy.tracklist.add({uri: result[0].uri}).then((e)=>{
Logger.log(`Playing news podcast`)
mopidy.playback.play()
})
})
})
}
playPlaylist(uri, volume) {
var mopidy = this.mopidy
this.playing = true
if (volume) {
this.volume = volume
mopidy.mixer.setVolume({volume: this.volume})
} else {
mopidy.mixer.setVolume({volume: this.volume})
}
mopidy.tracklist.clear().then(()=>{
mopidy.tracklist.add({uri: uri}).then((result)=>{
mopidy.tracklist.shuffle().then(()=>{
Logger.log(`Playing music playlist`)
mopidy.playback.play()
})
})
})
}
stop() {
var mopidy = this.mopidy
Logger.log(`Stopping music`)
this.playing = false
mopidy.playback.stop()
}
fadeDown(steps, ms, downTo) {
var mopidy = this.mopidy
if (!steps) steps = 5
if (!ms) ms = 500
if (!downTo) downTo = 72
var thisVolume = this.volume
var promise = new Promise( function (resolve, reject) {
mopidy.mixer.getVolume().then((e)=>{
thisVolume = e
for (var i = 1; i <= steps; i++) {
var volumeChunks = (thisVolume - downTo) / steps
var volume = Math.round(thisVolume - (volumeChunks * i))
if (i == steps) {
setTimeout(function(){
mopidy.mixer.setVolume({volume: Number(this)}).then(()=>{
resolve(true)
})}.bind(volume), (ms/steps)*i)
} else {
setTimeout(function(){
mopidy.mixer.setVolume({volume: Number(this)}).then(()=>{
})}.bind(volume), (ms/steps)*i)
}
}
})
})
return promise
}
fadeUp(steps, ms, to) {
var mopidy = this.mopidy
if (!steps) steps = 10
if (!ms) ms = 500
if (to) this.volume = to
mopidy.mixer.getVolume().then((e)=>{
for (var i = 1; i <= steps; i++) {
var volumeChunks = Math.abs(this.volume - e) / steps
var volume = Math.round(e + (volumeChunks * i))
setTimeout(function(){
mopidy.mixer.setVolume({volume: Number(this)})
}.bind(volume), (ms/steps)*i)
}
})
}
getState() {
var mopidy = this.mopidy
var thisObj = this;
var promise = new Promise( function (resolve, reject) {
mopidy.playback.getState().then((e)=>{
if (e !== 'playing') {
if (thisObj.playing) {
mopidy.mixer.setVolume({volume: Number(85)})
thisObj.volume = 85
}
thisObj.playing = false
} else {
thisObj.playing = true
}
mopidy.mixer.getVolume().then((data)=>{
thisObj.volume = data
})
})
})
return promise
}
}
module.exports = MusicPlayer