2022-02-23 00:33:55 +01:00
|
|
|
import Hls from 'hls.js'
|
|
|
|
import EventEmitter from 'events'
|
|
|
|
|
2022-05-31 02:26:53 +02:00
|
|
|
export default class LocalAudioPlayer extends EventEmitter {
|
2022-02-23 00:33:55 +01:00
|
|
|
constructor(ctx) {
|
|
|
|
super()
|
|
|
|
|
|
|
|
this.ctx = ctx
|
|
|
|
this.player = null
|
|
|
|
|
2022-03-13 02:59:35 +01:00
|
|
|
this.libraryItem = null
|
2022-02-23 00:33:55 +01:00
|
|
|
this.audioTracks = []
|
|
|
|
this.currentTrackIndex = 0
|
2022-03-18 01:10:47 +01:00
|
|
|
this.isHlsTranscode = null
|
2022-02-23 00:33:55 +01:00
|
|
|
this.hlsInstance = null
|
|
|
|
this.usingNativeplayer = false
|
2022-03-05 22:37:30 +01:00
|
|
|
this.startTime = 0
|
|
|
|
this.trackStartTime = 0
|
2022-02-23 00:33:55 +01:00
|
|
|
this.playWhenReady = false
|
|
|
|
this.defaultPlaybackRate = 1
|
|
|
|
|
2022-04-16 19:37:10 +02:00
|
|
|
this.playableMimeTypes = []
|
2022-03-05 22:37:30 +01:00
|
|
|
|
2022-02-23 00:33:55 +01:00
|
|
|
this.initialize()
|
|
|
|
}
|
|
|
|
|
|
|
|
get currentTrack() {
|
|
|
|
return this.audioTracks[this.currentTrackIndex] || {}
|
|
|
|
}
|
|
|
|
|
2023-03-19 22:41:49 +01:00
|
|
|
get hslContentUrl() {
|
|
|
|
// the hls stream playlist is the same for all tracks
|
|
|
|
return this.audioTracks[0].relativeContentUrl
|
|
|
|
}
|
|
|
|
|
2022-02-23 00:33:55 +01:00
|
|
|
initialize() {
|
|
|
|
if (document.getElementById('audio-player')) {
|
|
|
|
document.getElementById('audio-player').remove()
|
|
|
|
}
|
|
|
|
var audioEl = document.createElement('audio')
|
|
|
|
audioEl.id = 'audio-player'
|
|
|
|
audioEl.style.display = 'none'
|
|
|
|
document.body.appendChild(audioEl)
|
|
|
|
this.player = audioEl
|
|
|
|
|
|
|
|
this.player.addEventListener('play', this.evtPlay.bind(this))
|
|
|
|
this.player.addEventListener('pause', this.evtPause.bind(this))
|
|
|
|
this.player.addEventListener('progress', this.evtProgress.bind(this))
|
2022-03-05 22:37:30 +01:00
|
|
|
this.player.addEventListener('ended', this.evtEnded.bind(this))
|
2022-02-23 00:33:55 +01:00
|
|
|
this.player.addEventListener('error', this.evtError.bind(this))
|
|
|
|
this.player.addEventListener('loadedmetadata', this.evtLoadedMetadata.bind(this))
|
|
|
|
this.player.addEventListener('timeupdate', this.evtTimeupdate.bind(this))
|
2022-03-05 22:37:30 +01:00
|
|
|
|
2022-10-21 00:24:51 +02:00
|
|
|
var mimeTypes = ['audio/flac', 'audio/mpeg', 'audio/mp4', 'audio/ogg', 'audio/aac', 'audio/x-ms-wma', 'audio/x-aiff', 'audio/webm']
|
2022-04-16 19:37:10 +02:00
|
|
|
var mimeTypeCanPlayMap = {}
|
2022-03-05 22:37:30 +01:00
|
|
|
mimeTypes.forEach((mt) => {
|
2022-04-16 19:37:10 +02:00
|
|
|
var canPlay = this.player.canPlayType(mt)
|
|
|
|
mimeTypeCanPlayMap[mt] = canPlay
|
|
|
|
if (canPlay) this.playableMimeTypes.push(mt)
|
2022-03-05 22:37:30 +01:00
|
|
|
})
|
2022-04-16 19:37:10 +02:00
|
|
|
console.log(`[LocalPlayer] Supported mime types`, mimeTypeCanPlayMap, this.playableMimeTypes)
|
2022-02-23 00:33:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
evtPlay() {
|
|
|
|
this.emit('stateChange', 'PLAYING')
|
|
|
|
}
|
|
|
|
evtPause() {
|
|
|
|
this.emit('stateChange', 'PAUSED')
|
|
|
|
}
|
|
|
|
evtProgress() {
|
|
|
|
var lastBufferTime = this.getLastBufferedTime()
|
|
|
|
this.emit('buffertimeUpdate', lastBufferTime)
|
|
|
|
}
|
2022-03-05 22:37:30 +01:00
|
|
|
evtEnded() {
|
|
|
|
if (this.currentTrackIndex < this.audioTracks.length - 1) {
|
|
|
|
console.log(`[LocalPlayer] Track ended - loading next track ${this.currentTrackIndex + 1}`)
|
|
|
|
// Has next track
|
|
|
|
this.currentTrackIndex++
|
|
|
|
this.startTime = this.currentTrack.startOffset
|
|
|
|
this.loadCurrentTrack()
|
|
|
|
} else {
|
|
|
|
console.log(`[LocalPlayer] Ended`)
|
2022-05-29 19:55:14 +02:00
|
|
|
this.emit('finished')
|
2022-03-05 22:37:30 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-23 00:33:55 +01:00
|
|
|
evtError(error) {
|
|
|
|
console.error('Player error', error)
|
2022-03-05 22:37:30 +01:00
|
|
|
this.emit('error', error)
|
2022-02-23 00:33:55 +01:00
|
|
|
}
|
|
|
|
evtLoadedMetadata(data) {
|
2022-03-18 01:10:47 +01:00
|
|
|
if (!this.isHlsTranscode) {
|
2022-03-05 22:37:30 +01:00
|
|
|
this.player.currentTime = this.trackStartTime
|
|
|
|
}
|
|
|
|
|
2022-02-23 00:33:55 +01:00
|
|
|
this.emit('stateChange', 'LOADED')
|
2022-06-04 19:07:38 +02:00
|
|
|
|
2022-02-23 00:33:55 +01:00
|
|
|
if (this.playWhenReady) {
|
|
|
|
this.playWhenReady = false
|
|
|
|
this.play()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
evtTimeupdate() {
|
|
|
|
if (this.player.paused) {
|
|
|
|
this.emit('timeupdate', this.getCurrentTime())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
this.destroyHlsInstance()
|
|
|
|
if (this.player) {
|
|
|
|
this.player.remove()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-18 01:10:47 +01:00
|
|
|
set(libraryItem, tracks, isHlsTranscode, startTime, playWhenReady = false) {
|
2022-03-13 02:59:35 +01:00
|
|
|
this.libraryItem = libraryItem
|
2022-02-23 00:33:55 +01:00
|
|
|
this.audioTracks = tracks
|
2022-03-18 01:10:47 +01:00
|
|
|
this.isHlsTranscode = isHlsTranscode
|
2022-02-23 00:33:55 +01:00
|
|
|
this.playWhenReady = playWhenReady
|
2022-03-05 22:37:30 +01:00
|
|
|
this.startTime = startTime
|
|
|
|
|
2022-02-23 00:33:55 +01:00
|
|
|
if (this.hlsInstance) {
|
|
|
|
this.destroyHlsInstance()
|
|
|
|
}
|
|
|
|
|
2022-03-18 01:10:47 +01:00
|
|
|
if (this.isHlsTranscode) {
|
2022-03-05 22:37:30 +01:00
|
|
|
this.setHlsStream()
|
|
|
|
} else {
|
|
|
|
this.setDirectPlay()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setHlsStream() {
|
|
|
|
this.trackStartTime = 0
|
2022-02-23 00:33:55 +01:00
|
|
|
|
|
|
|
// iOS does not support Media Elements but allows for HLS in the native audio player
|
|
|
|
if (!Hls.isSupported()) {
|
|
|
|
console.warn('HLS is not supported - fallback to using audio element')
|
|
|
|
this.usingNativeplayer = true
|
2023-03-19 22:41:49 +01:00
|
|
|
this.player.src = this.hslContentUrl
|
2022-03-05 22:37:30 +01:00
|
|
|
this.player.currentTime = this.startTime
|
2022-02-23 00:33:55 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var hlsOptions = {
|
2022-03-05 22:37:30 +01:00
|
|
|
startPosition: this.startTime || -1
|
2022-02-23 00:33:55 +01:00
|
|
|
// No longer needed because token is put in a query string
|
|
|
|
// xhrSetup: (xhr) => {
|
|
|
|
// xhr.setRequestHeader('Authorization', `Bearer ${this.token}`)
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
this.hlsInstance = new Hls(hlsOptions)
|
|
|
|
|
|
|
|
this.hlsInstance.attachMedia(this.player)
|
|
|
|
this.hlsInstance.on(Hls.Events.MEDIA_ATTACHED, () => {
|
2023-03-19 22:41:49 +01:00
|
|
|
this.hlsInstance.loadSource(this.hslContentUrl)
|
2022-02-23 00:33:55 +01:00
|
|
|
|
|
|
|
this.hlsInstance.on(Hls.Events.MANIFEST_PARSED, () => {
|
|
|
|
console.log('[HLS] Manifest Parsed')
|
|
|
|
})
|
|
|
|
|
|
|
|
this.hlsInstance.on(Hls.Events.ERROR, (e, data) => {
|
|
|
|
console.error('[HLS] Error', data.type, data.details, data)
|
|
|
|
if (data.details === Hls.ErrorDetails.BUFFER_STALLED_ERROR) {
|
|
|
|
console.error('[HLS] BUFFER STALLED ERROR')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.hlsInstance.on(Hls.Events.DESTROYING, () => {
|
|
|
|
console.log('[HLS] Destroying HLS Instance')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-05 22:37:30 +01:00
|
|
|
setDirectPlay() {
|
|
|
|
// Set initial track and track time offset
|
|
|
|
var trackIndex = this.audioTracks.findIndex(t => this.startTime >= t.startOffset && this.startTime < (t.startOffset + t.duration))
|
|
|
|
this.currentTrackIndex = trackIndex >= 0 ? trackIndex : 0
|
|
|
|
|
|
|
|
this.loadCurrentTrack()
|
|
|
|
}
|
|
|
|
|
|
|
|
loadCurrentTrack() {
|
|
|
|
if (!this.currentTrack) return
|
|
|
|
// When direct play track is loaded current time needs to be set
|
|
|
|
this.trackStartTime = Math.max(0, this.startTime - (this.currentTrack.startOffset || 0))
|
|
|
|
this.player.src = this.currentTrack.relativeContentUrl
|
|
|
|
console.log(`[LocalPlayer] Loading track src ${this.currentTrack.relativeContentUrl}`)
|
|
|
|
this.player.load()
|
|
|
|
}
|
|
|
|
|
2022-02-23 00:33:55 +01:00
|
|
|
destroyHlsInstance() {
|
|
|
|
if (!this.hlsInstance) return
|
|
|
|
if (this.hlsInstance.destroy) {
|
|
|
|
var temp = this.hlsInstance
|
|
|
|
temp.destroy()
|
|
|
|
}
|
|
|
|
this.hlsInstance = null
|
|
|
|
}
|
|
|
|
|
|
|
|
async resetStream(startTime) {
|
|
|
|
this.destroyHlsInstance()
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1000))
|
2022-03-18 01:10:47 +01:00
|
|
|
this.set(this.libraryItem, this.audioTracks, this.isHlsTranscode, startTime, true)
|
2022-02-23 00:33:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
playPause() {
|
|
|
|
if (!this.player) return
|
|
|
|
if (this.player.paused) this.play()
|
|
|
|
else this.pause()
|
|
|
|
}
|
|
|
|
|
|
|
|
play() {
|
2022-06-05 17:06:07 +02:00
|
|
|
this.playWhenReady = true
|
2022-02-23 00:33:55 +01:00
|
|
|
if (this.player) this.player.play()
|
|
|
|
}
|
|
|
|
|
|
|
|
pause() {
|
2022-06-05 17:06:07 +02:00
|
|
|
this.playWhenReady = false
|
2022-02-23 00:33:55 +01:00
|
|
|
if (this.player) this.player.pause()
|
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentTime() {
|
|
|
|
var currentTrackOffset = this.currentTrack.startOffset || 0
|
|
|
|
return this.player ? currentTrackOffset + this.player.currentTime : 0
|
|
|
|
}
|
|
|
|
|
|
|
|
getDuration() {
|
|
|
|
if (!this.audioTracks.length) return 0
|
|
|
|
var lastTrack = this.audioTracks[this.audioTracks.length - 1]
|
|
|
|
return lastTrack.startOffset + lastTrack.duration
|
|
|
|
}
|
|
|
|
|
|
|
|
setPlaybackRate(playbackRate) {
|
|
|
|
if (!this.player) return
|
|
|
|
this.defaultPlaybackRate = playbackRate
|
|
|
|
this.player.playbackRate = playbackRate
|
|
|
|
}
|
|
|
|
|
2022-06-04 19:07:38 +02:00
|
|
|
seek(time, playWhenReady) {
|
2022-02-23 00:33:55 +01:00
|
|
|
if (!this.player) return
|
2022-06-04 19:07:38 +02:00
|
|
|
|
|
|
|
this.playWhenReady = playWhenReady
|
|
|
|
|
2022-03-18 01:10:47 +01:00
|
|
|
if (this.isHlsTranscode) {
|
2022-03-05 22:37:30 +01:00
|
|
|
// Seeking HLS stream
|
|
|
|
var offsetTime = time - (this.currentTrack.startOffset || 0)
|
|
|
|
this.player.currentTime = Math.max(0, offsetTime)
|
|
|
|
} else {
|
|
|
|
// Seeking Direct play
|
|
|
|
if (time < this.currentTrack.startOffset || time > this.currentTrack.startOffset + this.currentTrack.duration) {
|
|
|
|
// Change Track
|
|
|
|
var trackIndex = this.audioTracks.findIndex(t => time >= t.startOffset && time < (t.startOffset + t.duration))
|
|
|
|
if (trackIndex >= 0) {
|
|
|
|
this.startTime = time
|
|
|
|
this.currentTrackIndex = trackIndex
|
|
|
|
|
|
|
|
if (!this.player.paused) {
|
|
|
|
// audio player playing so play when track loads
|
|
|
|
this.playWhenReady = true
|
|
|
|
}
|
|
|
|
this.loadCurrentTrack()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var offsetTime = time - (this.currentTrack.startOffset || 0)
|
|
|
|
this.player.currentTime = Math.max(0, offsetTime)
|
|
|
|
}
|
|
|
|
}
|
2022-02-23 00:33:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
setVolume(volume) {
|
|
|
|
if (!this.player) return
|
|
|
|
this.player.volume = volume
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Utils
|
|
|
|
isValidDuration(duration) {
|
|
|
|
if (duration && !isNaN(duration) && duration !== Number.POSITIVE_INFINITY && duration !== Number.NEGATIVE_INFINITY) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
getBufferedRanges() {
|
|
|
|
if (!this.player) return []
|
|
|
|
const ranges = []
|
|
|
|
const seekable = this.player.buffered || []
|
|
|
|
|
|
|
|
let offset = 0
|
|
|
|
|
|
|
|
for (let i = 0, length = seekable.length; i < length; i++) {
|
|
|
|
let start = seekable.start(i)
|
|
|
|
let end = seekable.end(i)
|
|
|
|
if (!this.isValidDuration(start)) {
|
|
|
|
start = 0
|
|
|
|
}
|
|
|
|
if (!this.isValidDuration(end)) {
|
|
|
|
end = 0
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ranges.push({
|
|
|
|
start: start + offset,
|
|
|
|
end: end + offset
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return ranges
|
|
|
|
}
|
|
|
|
|
|
|
|
getLastBufferedTime() {
|
|
|
|
var bufferedRanges = this.getBufferedRanges()
|
|
|
|
if (!bufferedRanges.length) return 0
|
|
|
|
|
|
|
|
var buff = bufferedRanges.find((buff) => buff.start < this.player.currentTime && buff.end > this.player.currentTime)
|
|
|
|
if (buff) return buff.end
|
|
|
|
|
|
|
|
var last = bufferedRanges[bufferedRanges.length - 1]
|
|
|
|
return last.end
|
|
|
|
}
|
|
|
|
}
|