AudioPlayerMonitor = class {
constructor(audio, monitorFn) {
this._audio = audio
this._monitorFn = monitorFn
this.startMonitoring = this.startMonitoring.bind(this)
this.stopMonitoring = this.stopMonitoring.bind(this)
this.tick = this.tick.bind(this)
audio.addEventListener('play', this.startMonitoring)
audio.addEventListener('pause', this.stopMonitoring)
audio.addEventListener('seeked', () => this.tick(false))
if (!this._audio.paused) this.startMonitoring()
}
startMonitoring() {
this.tick()
}
stopMonitoring() {
cancelAnimationFrame(this._nextFrame)
}
tick(requestNextFrame = true) {
this._monitorFn(this._audio)
if (requestNextFrame) this._nextFrame = requestAnimationFrame(this.tick)
}
}