mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-07-12 12:21:34 +00:00
119 lines
3.3 KiB
JavaScript
119 lines
3.3 KiB
JavaScript
import EventEmitter from 'events'
|
|
|
|
export default class DLNAPlayer extends EventEmitter {
|
|
constructor(ctx) {
|
|
super()
|
|
|
|
this.ctx = ctx
|
|
this.player = null
|
|
this.playerController = null
|
|
this.state = null
|
|
this.libraryItem = null
|
|
this.audioTracks = []
|
|
this.currentTrackIndex = 0
|
|
this.isHlsTranscode = null
|
|
this.currentTime = 0
|
|
this.playWhenReady = false
|
|
this.defaultPlaybackRate = 1
|
|
this.paused
|
|
this.currentTime = null
|
|
// TODO: Use canDisplayType on receiver to check mime types
|
|
this.playableMimeTypes = ['audio/flac', 'audio/mpeg', 'audio/mp4', 'audio/ogg', 'audio/aac', 'audio/x-ms-wma', 'audio/x-aiff', 'audio/webm']
|
|
|
|
this.coverUrl = ''
|
|
this.castPlayerState = 'IDLE'
|
|
|
|
// Supported audio codecs for chromecast
|
|
|
|
this.supportedAudioCodecs = ['opus', 'mp3', 'aac', 'flac', 'webma', 'wav']
|
|
|
|
this.initialize()
|
|
}
|
|
|
|
get currentTrack() {
|
|
return this.audioTracks[this.currentTrackIndex] || {}
|
|
}
|
|
|
|
initialize() {
|
|
this.ctx.$root.socket.on('dlna_status', (data) => this.setStatus(data))
|
|
//this.player = this.ctx.$root.castPlayer
|
|
}
|
|
|
|
evtMediaInfoChanged() {}
|
|
|
|
destroy() {
|
|
this.ctx.$root.socket.emit('dlna_exit')
|
|
}
|
|
|
|
async set(libraryItem, tracks, isHlsTranscode, startTime, playWhenReady = false) {
|
|
this.libraryItem = libraryItem
|
|
this.audioTracks = tracks
|
|
this.isHlsTranscode = isHlsTranscode
|
|
this.playWhenReady = playWhenReady
|
|
this.currentTime = startTime
|
|
console.log('Tracks: ')
|
|
console.log(tracks.find((at) => at.startOffset <= startTime && at.startOffset + at.duration > startTime))
|
|
var coverImg = this.ctx.$store.getters['globals/getLibraryItemCoverSrc'](libraryItem)
|
|
if (process.env.NODE_ENV === 'development') {
|
|
this.coverUrl = coverImg
|
|
} else {
|
|
this.coverUrl = `${window.location.origin}${coverImg}`
|
|
}
|
|
var serverAddress = window.origin
|
|
if (this.$isDev) serverAddress = `http://localhost:3333${this.$config.routerBasePath}`
|
|
this.ctx.$root.socket.emit('dlna_start', this.ctx.$store.state.globals.dlnaDevice, this.audioTracks, startTime, serverAddress)
|
|
}
|
|
|
|
resetStream(startTime) {}
|
|
|
|
playPause() {
|
|
if (this.state == 'PLAYING') {
|
|
this.pause()
|
|
} else {
|
|
this.play()
|
|
}
|
|
}
|
|
|
|
play() {
|
|
this.ctx.$root.socket.emit('dlna_play')
|
|
}
|
|
|
|
pause() {
|
|
this.ctx.$root.socket.emit('dlna_pause')
|
|
}
|
|
|
|
getCurrentTime() {
|
|
//var currentTrackOffset = this.currentTrack.startOffset || 0
|
|
return 0 //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) {
|
|
this.defaultPlaybackRate = playbackRate
|
|
}
|
|
|
|
async seek(time, playWhenReady) {}
|
|
setStatus(data) {
|
|
console.log(data)
|
|
if (this.state != data.status) {
|
|
this.state = data.status
|
|
this.emit('stateChange', this.state)
|
|
}
|
|
this.currentTrackIndex = data.track_idx
|
|
this.currentTime = data.pos
|
|
console.log(this.currentTrackIndex, this.currentTime)
|
|
}
|
|
|
|
getCurrentTime() {
|
|
return this.currentTime + this.audioTracks[this.currentTrackIndex].startOffset
|
|
}
|
|
seek(time) {
|
|
this.ctx.$root.socket.emit('dlna_seek', time)
|
|
}
|
|
setVolume(volume) {}
|
|
}
|