2019-02-14 06:19:29 -05:00
|
|
|
/**
|
|
|
|
* Audio handler object
|
|
|
|
* Taken from https://github.com/rainner/soma-fm-player (released under MIT licence)
|
|
|
|
*/
|
|
|
|
export default {
|
|
|
|
_audio: new Audio(),
|
2019-02-19 04:33:07 -05:00
|
|
|
_context: null,
|
2019-02-14 06:19:29 -05:00
|
|
|
_source: null,
|
|
|
|
_gain: null,
|
|
|
|
|
|
|
|
// setup audio routing
|
|
|
|
setupAudio () {
|
2021-01-10 01:51:50 -05:00
|
|
|
const AudioContext = window.AudioContext || window.webkitAudioContext
|
2019-02-19 04:33:07 -05:00
|
|
|
this._context = new AudioContext()
|
2019-02-14 06:19:29 -05:00
|
|
|
this._source = this._context.createMediaElementSource(this._audio)
|
|
|
|
this._gain = this._context.createGain()
|
|
|
|
|
|
|
|
this._source.connect(this._gain)
|
|
|
|
this._gain.connect(this._context.destination)
|
|
|
|
|
|
|
|
this._audio.addEventListener('canplaythrough', e => {
|
|
|
|
this._audio.play()
|
|
|
|
})
|
2019-02-18 06:47:09 -05:00
|
|
|
this._audio.addEventListener('canplay', e => {
|
|
|
|
this._audio.play()
|
|
|
|
})
|
2019-02-14 06:19:29 -05:00
|
|
|
return this._audio
|
|
|
|
},
|
|
|
|
|
|
|
|
// set audio volume
|
|
|
|
setVolume (volume) {
|
|
|
|
if (!this._gain) return
|
|
|
|
volume = parseFloat(volume) || 0.0
|
|
|
|
volume = (volume < 0) ? 0 : volume
|
|
|
|
volume = (volume > 1) ? 1 : volume
|
|
|
|
this._gain.gain.value = volume
|
|
|
|
},
|
|
|
|
|
|
|
|
// play audio source url
|
|
|
|
playSource (source) {
|
|
|
|
this.stopAudio()
|
2019-02-18 06:47:09 -05:00
|
|
|
this._context.resume().then(() => {
|
|
|
|
this._audio.src = String(source || '') + '?x=' + Date.now()
|
|
|
|
this._audio.crossOrigin = 'anonymous'
|
|
|
|
this._audio.load()
|
|
|
|
})
|
2019-02-14 06:19:29 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// stop playing audio
|
|
|
|
stopAudio () {
|
|
|
|
try { this._audio.pause() } catch (e) {}
|
|
|
|
try { this._audio.stop() } catch (e) {}
|
|
|
|
try { this._audio.close() } catch (e) {}
|
|
|
|
}
|
|
|
|
}
|