-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
98 lines (83 loc) · 2.42 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
var Nanobus = require('nanobus')
var window = require('global/window')
var get = require('lodash.get')
var setTimeout = window.setTimeout
var clearTimeout = window.clearTimeout
class AudioPlayer extends Nanobus {
constructor (audioNode, state) {
super('Hyperaudio')
this.audio = audioNode
this.audio.volume = state.volume
this.audio.muted = state.muted
this.audio.src = get(state.al, 'currentTrack.filepath') || ''
this.seeking = false
this.seekDebounceTimer = null
this.timeupdate = null
this._endedListener = this.audio.addEventListener('ended',
() => { if (!this.seeking) this.emit('ended') })
this._timeListener = this.audio.addEventListener('timeupdate', (ev) => {
if (this.seeking) return
var timeupdate = Math.floor(this.audio.currentTime)
if (this.timeupdate === timeupdate) return
this.timeupdate = timeupdate
this.emit('timeupdate', this.timeupdate)
})
this.emit('initialized', this)
}
seekDebounce () {
this.seeking = true
if (this.seekDebounceTimer) clearTimeout(this.seekDebounceTimer)
this.seekDebounceTimer = setTimeout(() => {
this.emit('debounce cleared')
this.seeking = false
this.seekDebounceTimer = null
// TODO: check if we are at the end and we lost the endedEvent
}, 1000)
}
load (src) {
this.emit('loading', src)
this.audio.src = src || ''
}
play () {
this.emit('playing')
this.audio.play()
}
pause () {
this.emit('paused')
this.audio.pause()
}
volume (lev) {
this.emit('volume', lev)
this.audio.volume = lev
}
mute () {
this.emit('muted')
this.audio.muted = true
}
unmute () {
this.emit('unmuted')
this.audio.muted = false
}
seek (newTime) {
this.seekDebounce()
this.emit('seek', newTime)
this.audio.currentTime = newTime
}
}
module.exports = AudioPlayer
// TODO: move to main thread
// function notify () {
// var key = this.trackOrder[this.currentIndex]
// var track = this.trackDict[key]
// var artwork = fileUrlFromPath(track.artwork ? track.artwork : path.resolve(__dirname, '../../static/splash-mini.png'))
//
// new window.Notification(track.title, { // eslint-disable-line no-new
// // TODO: placeholder. ideally this is album art
// icon: artwork,
// title: track.title,
// body: track.artist[0],
// tag: 'nowPlaying',
// silent: true,
// requireInteraction: false
// })
// }