2021-11-01 21:06:51 -05:00
|
|
|
<template>
|
|
|
|
<div>
|
2022-04-05 19:44:14 -05:00
|
|
|
<app-audio-player ref="audioPlayer" :playing.sync="isPlaying" :bookmarks="bookmarks" :sleep-timer-running="isSleepTimerRunning" :sleep-time-remaining="sleepTimeRemaining" @selectPlaybackSpeed="showPlaybackSpeedModal = true" @updateTime="(t) => (currentTime = t)" @showSleepTimer="showSleepTimer" @showBookmarks="showBookmarks" />
|
2021-11-01 21:06:51 -05:00
|
|
|
|
2021-12-11 13:20:20 -06:00
|
|
|
<modals-playback-speed-modal v-model="showPlaybackSpeedModal" :playback-rate.sync="playbackSpeed" @update:playbackRate="updatePlaybackSpeed" @change="changePlaybackSpeed" />
|
2021-11-27 12:15:17 -06:00
|
|
|
<modals-sleep-timer-modal v-model="showSleepTimerModal" :current-time="sleepTimeRemaining" :sleep-timer-running="isSleepTimerRunning" :current-end-of-chapter-time="currentEndOfChapterTime" @change="selectSleepTimeout" @cancel="cancelSleepTimer" @increase="increaseSleepTimer" @decrease="decreaseSleepTimer" />
|
2022-04-03 14:37:44 -05:00
|
|
|
<modals-bookmarks-modal v-model="showBookmarksModal" :bookmarks="bookmarks" :current-time="currentTime" @select="selectBookmark" />
|
2021-11-01 21:06:51 -05:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2022-04-04 19:08:27 -05:00
|
|
|
import { AbsAudioPlayer } from '@/plugins/capacitor'
|
2021-11-01 21:06:51 -05:00
|
|
|
|
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
2021-11-26 18:27:18 -06:00
|
|
|
isPlaying: false,
|
2021-11-01 21:06:51 -05:00
|
|
|
audioPlayerReady: false,
|
|
|
|
stream: null,
|
|
|
|
download: null,
|
|
|
|
showPlaybackSpeedModal: false,
|
2021-11-02 19:44:42 -05:00
|
|
|
showBookmarksModal: false,
|
2021-11-01 21:06:51 -05:00
|
|
|
showSleepTimerModal: false,
|
|
|
|
playbackSpeed: 1,
|
|
|
|
currentTime: 0,
|
|
|
|
isSleepTimerRunning: false,
|
2021-11-26 18:27:18 -06:00
|
|
|
sleepTimerEndTime: 0,
|
2022-04-03 14:37:44 -05:00
|
|
|
sleepTimeRemaining: 0,
|
2021-11-01 21:06:51 -05:00
|
|
|
onSleepTimerEndedListener: null,
|
2021-11-26 18:27:18 -06:00
|
|
|
onSleepTimerSetListener: null,
|
2021-11-01 21:06:51 -05:00
|
|
|
sleepInterval: null,
|
2022-04-02 19:43:43 -05:00
|
|
|
currentEndOfChapterTime: 0
|
2021-11-01 21:06:51 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
socketConnected(newVal) {
|
|
|
|
if (newVal) {
|
|
|
|
console.log('Socket Connected set listeners')
|
|
|
|
this.setListeners()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
2021-11-02 19:44:42 -05:00
|
|
|
bookmarks() {
|
2022-04-02 12:12:00 -05:00
|
|
|
// return this.$store.getters['user/getUserBookmarksForItem'](this.)
|
|
|
|
return []
|
2021-11-01 21:06:51 -05:00
|
|
|
},
|
|
|
|
socketConnected() {
|
|
|
|
return this.$store.state.socketConnected
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2021-11-02 19:44:42 -05:00
|
|
|
showBookmarks() {
|
|
|
|
this.showBookmarksModal = true
|
|
|
|
},
|
|
|
|
selectBookmark(bookmark) {
|
|
|
|
this.showBookmarksModal = false
|
|
|
|
if (!bookmark || isNaN(bookmark.time)) return
|
|
|
|
var bookmarkTime = Number(bookmark.time)
|
|
|
|
if (this.$refs.audioPlayer) {
|
|
|
|
this.$refs.audioPlayer.seek(bookmarkTime)
|
|
|
|
}
|
|
|
|
},
|
2021-11-01 21:06:51 -05:00
|
|
|
onSleepTimerEnded({ value: currentPosition }) {
|
|
|
|
this.isSleepTimerRunning = false
|
|
|
|
if (currentPosition) {
|
|
|
|
console.log('Sleep Timer Ended Current Position: ' + currentPosition)
|
|
|
|
var currentTime = Math.floor(currentPosition / 1000)
|
2022-04-02 12:12:00 -05:00
|
|
|
// TODO: Was syncing to the server here before
|
2021-11-01 21:06:51 -05:00
|
|
|
}
|
|
|
|
},
|
2022-02-03 09:19:27 -06:00
|
|
|
onSleepTimerSet({ value: sleepTimeRemaining }) {
|
|
|
|
console.log('SLEEP TIMER SET', sleepTimeRemaining)
|
|
|
|
if (sleepTimeRemaining === 0) {
|
2021-11-26 18:27:18 -06:00
|
|
|
console.log('Sleep timer canceled')
|
|
|
|
this.isSleepTimerRunning = false
|
|
|
|
} else {
|
|
|
|
this.isSleepTimerRunning = true
|
|
|
|
}
|
|
|
|
|
2022-02-03 09:19:27 -06:00
|
|
|
this.sleepTimeRemaining = sleepTimeRemaining
|
2021-11-26 18:27:18 -06:00
|
|
|
},
|
2021-11-01 21:06:51 -05:00
|
|
|
showSleepTimer() {
|
2022-04-02 12:12:00 -05:00
|
|
|
if (this.$refs.audioPlayer && this.$refs.audioPlayer.currentChapter) {
|
|
|
|
this.currentEndOfChapterTime = Math.floor(this.$refs.audioPlayer.currentChapter.end)
|
2021-11-01 21:06:51 -05:00
|
|
|
} else {
|
|
|
|
this.currentEndOfChapterTime = 0
|
|
|
|
}
|
|
|
|
this.showSleepTimerModal = true
|
|
|
|
},
|
|
|
|
async selectSleepTimeout({ time, isChapterTime }) {
|
|
|
|
console.log('Setting sleep timer', time, isChapterTime)
|
2022-04-04 19:08:27 -05:00
|
|
|
var res = await AbsAudioPlayer.setSleepTimer({ time: String(time), isChapterTime })
|
2021-11-01 21:06:51 -05:00
|
|
|
if (!res.success) {
|
|
|
|
return this.$toast.error('Sleep timer did not set, invalid time')
|
|
|
|
}
|
|
|
|
},
|
2021-11-27 12:15:17 -06:00
|
|
|
increaseSleepTimer() {
|
|
|
|
// Default time to increase = 5 min
|
2022-04-04 19:08:27 -05:00
|
|
|
AbsAudioPlayer.increaseSleepTime({ time: '300000' })
|
2021-11-27 12:15:17 -06:00
|
|
|
},
|
|
|
|
decreaseSleepTimer() {
|
2022-04-04 19:08:27 -05:00
|
|
|
AbsAudioPlayer.decreaseSleepTime({ time: '300000' })
|
2021-11-27 12:15:17 -06:00
|
|
|
},
|
2021-11-01 21:06:51 -05:00
|
|
|
async cancelSleepTimer() {
|
|
|
|
console.log('Canceling sleep timer')
|
2022-04-04 19:08:27 -05:00
|
|
|
await AbsAudioPlayer.cancelSleepTimer()
|
2021-11-01 21:06:51 -05:00
|
|
|
},
|
2022-04-02 12:12:00 -05:00
|
|
|
streamClosed() {
|
2021-11-01 21:06:51 -05:00
|
|
|
console.log('Stream Closed')
|
|
|
|
},
|
|
|
|
streamProgress(data) {
|
|
|
|
if (!data.numSegments) return
|
|
|
|
var chunks = data.chunks
|
|
|
|
if (this.$refs.audioPlayer) {
|
|
|
|
this.$refs.audioPlayer.setChunksReady(chunks, data.numSegments)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
streamReady() {
|
|
|
|
console.log('[StreamContainer] Stream Ready')
|
|
|
|
if (this.$refs.audioPlayer) {
|
|
|
|
this.$refs.audioPlayer.setStreamReady()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
streamReset({ streamId, startTime }) {
|
2022-04-02 12:12:00 -05:00
|
|
|
console.log('received stream reset', streamId, startTime)
|
2021-11-01 21:06:51 -05:00
|
|
|
if (this.$refs.audioPlayer) {
|
|
|
|
if (this.stream && this.stream.id === streamId) {
|
|
|
|
this.$refs.audioPlayer.resetStream(startTime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-12-11 13:20:20 -06:00
|
|
|
updatePlaybackSpeed(speed) {
|
2021-11-02 19:44:42 -05:00
|
|
|
if (this.$refs.audioPlayer) {
|
2021-12-11 13:20:20 -06:00
|
|
|
console.log(`[AudioPlayerContainer] Update Playback Speed: ${speed}`)
|
2021-11-02 19:44:42 -05:00
|
|
|
this.$refs.audioPlayer.setPlaybackSpeed(speed)
|
|
|
|
}
|
2021-12-11 13:20:20 -06:00
|
|
|
},
|
|
|
|
changePlaybackSpeed(speed) {
|
|
|
|
console.log(`[AudioPlayerContainer] Change Playback Speed: ${speed}`)
|
2021-11-01 21:06:51 -05:00
|
|
|
this.$store.dispatch('user/updateUserSettings', { playbackRate: speed })
|
|
|
|
},
|
|
|
|
settingsUpdated(settings) {
|
2021-11-02 19:44:42 -05:00
|
|
|
console.log(`[AudioPlayerContainer] Settings Update | PlaybackRate: ${settings.playbackRate}`)
|
|
|
|
this.playbackSpeed = settings.playbackRate
|
2021-11-01 21:06:51 -05:00
|
|
|
if (this.$refs.audioPlayer && this.$refs.audioPlayer.currentPlaybackRate !== settings.playbackRate) {
|
2021-11-02 19:44:42 -05:00
|
|
|
console.log(`[AudioPlayerContainer] PlaybackRate Updated: ${this.playbackSpeed}`)
|
|
|
|
this.$refs.audioPlayer.setPlaybackSpeed(this.playbackSpeed)
|
2021-11-01 21:06:51 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
setListeners() {
|
2022-04-03 14:24:17 -05:00
|
|
|
// if (!this.$server.socket) {
|
|
|
|
// console.error('Invalid server socket not set')
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// this.$server.socket.on('stream_open', this.streamOpen)
|
|
|
|
// this.$server.socket.on('stream_closed', this.streamClosed)
|
|
|
|
// this.$server.socket.on('stream_progress', this.streamProgress)
|
|
|
|
// this.$server.socket.on('stream_ready', this.streamReady)
|
|
|
|
// this.$server.socket.on('stream_reset', this.streamReset)
|
2021-12-05 18:31:47 -06:00
|
|
|
},
|
|
|
|
closeStreamOnly() {
|
2022-04-02 12:12:00 -05:00
|
|
|
// If user logs out or disconnects from server and not playing local
|
|
|
|
if (this.$refs.audioPlayer && !this.$refs.audioPlayer.isLocalPlayMethod) {
|
|
|
|
this.$refs.audioPlayer.terminateStream()
|
2021-12-05 18:31:47 -06:00
|
|
|
}
|
2022-03-23 17:59:14 -05:00
|
|
|
},
|
|
|
|
async playLibraryItem(libraryItemId) {
|
2022-04-04 19:08:27 -05:00
|
|
|
AbsAudioPlayer.prepareLibraryItem({ libraryItemId, playWhenReady: true })
|
2022-03-28 19:53:53 -05:00
|
|
|
.then((data) => {
|
|
|
|
console.log('TEST library item play response', JSON.stringify(data))
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error('TEST failed', error)
|
|
|
|
})
|
2021-11-01 21:06:51 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted() {
|
2022-04-04 19:08:27 -05:00
|
|
|
this.onSleepTimerEndedListener = AbsAudioPlayer.addListener('onSleepTimerEnded', this.onSleepTimerEnded)
|
|
|
|
this.onSleepTimerSetListener = AbsAudioPlayer.addListener('onSleepTimerSet', this.onSleepTimerSet)
|
2021-11-01 21:06:51 -05:00
|
|
|
|
|
|
|
this.playbackSpeed = this.$store.getters['user/getUserSetting']('playbackRate')
|
2021-11-02 19:44:42 -05:00
|
|
|
console.log(`[AudioPlayerContainer] Init Playback Speed: ${this.playbackSpeed}`)
|
2021-11-01 21:06:51 -05:00
|
|
|
|
|
|
|
this.setListeners()
|
2022-03-23 17:59:14 -05:00
|
|
|
this.$eventBus.$on('play-item', this.playLibraryItem)
|
2022-04-02 12:12:00 -05:00
|
|
|
this.$eventBus.$on('close-stream', this.closeStreamOnly)
|
2021-11-01 21:06:51 -05:00
|
|
|
this.$store.commit('user/addSettingsListener', { id: 'streamContainer', meth: this.settingsUpdated })
|
|
|
|
},
|
|
|
|
beforeDestroy() {
|
|
|
|
if (this.onSleepTimerEndedListener) this.onSleepTimerEndedListener.remove()
|
2021-11-26 18:27:18 -06:00
|
|
|
if (this.onSleepTimerSetListener) this.onSleepTimerSetListener.remove()
|
2021-11-01 21:06:51 -05:00
|
|
|
|
2022-04-03 14:24:17 -05:00
|
|
|
// if (this.$server.socket) {
|
|
|
|
// this.$server.socket.off('stream_open', this.streamOpen)
|
|
|
|
// this.$server.socket.off('stream_closed', this.streamClosed)
|
|
|
|
// this.$server.socket.off('stream_progress', this.streamProgress)
|
|
|
|
// this.$server.socket.off('stream_ready', this.streamReady)
|
|
|
|
// this.$server.socket.off('stream_reset', this.streamReset)
|
|
|
|
// }
|
2022-03-23 17:59:14 -05:00
|
|
|
this.$eventBus.$off('play-item', this.playLibraryItem)
|
2022-04-02 12:12:00 -05:00
|
|
|
this.$eventBus.$off('close-stream', this.closeStreamOnly)
|
2021-11-01 21:06:51 -05:00
|
|
|
this.$store.commit('user/removeSettingsListener', 'streamContainer')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|