2024-06-22 23:42:13 +02:00
|
|
|
<template>
|
2024-06-29 22:05:35 +02:00
|
|
|
<div id="page-wrapper" class="w-full h-screen max-h-screen overflow-hidden">
|
2024-06-22 23:42:13 +02:00
|
|
|
<div class="w-full h-full flex items-center justify-center">
|
2024-06-29 22:05:35 +02:00
|
|
|
<div class="w-full p-2 sm:p-4 md:p-8">
|
2024-06-29 23:15:55 +02:00
|
|
|
<div v-if="!isMobileLandscape" :style="{ width: coverWidth + 'px', height: coverHeight + 'px' }" class="mx-auto overflow-hidden rounded-xl my-2">
|
2024-06-29 22:05:35 +02:00
|
|
|
<img :src="coverUrl" class="object-contain w-full h-full" />
|
|
|
|
</div>
|
2024-06-29 23:15:55 +02:00
|
|
|
<p class="text-2xl lg:text-3xl font-semibold text-center mb-1 line-clamp-2">{{ mediaItemShare.playbackSession.displayTitle || 'No title' }}</p>
|
|
|
|
<p v-if="mediaItemShare.playbackSession.displayAuthor" class="text-lg lg:text-xl text-slate-400 font-semibold text-center mb-1 truncate">{{ mediaItemShare.playbackSession.displayAuthor }}</p>
|
2024-06-27 00:03:12 +02:00
|
|
|
|
2024-06-29 22:05:35 +02:00
|
|
|
<div class="w-full pt-16">
|
2024-06-29 00:01:28 +02:00
|
|
|
<player-ui ref="audioPlayer" :chapters="chapters" :paused="isPaused" :loading="!hasLoaded" :is-podcast="false" hide-bookmarks hide-sleep-timer @playPause="playPause" @jumpForward="jumpForward" @jumpBackward="jumpBackward" @setVolume="setVolume" @setPlaybackRate="setPlaybackRate" @seek="seek" />
|
|
|
|
</div>
|
2024-06-27 00:03:12 +02:00
|
|
|
</div>
|
2024-06-22 23:42:13 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2024-06-27 00:03:12 +02:00
|
|
|
import LocalAudioPlayer from '../../players/LocalAudioPlayer'
|
|
|
|
|
2024-06-22 23:42:13 +02:00
|
|
|
export default {
|
|
|
|
layout: 'blank',
|
2024-06-30 22:31:27 +02:00
|
|
|
async asyncData({ params, error, app, query }) {
|
|
|
|
let endpoint = `/public/share/${params.slug}`
|
|
|
|
if (query.t && !isNaN(query.t)) {
|
|
|
|
endpoint += `?t=${query.t}`
|
|
|
|
}
|
2024-06-30 23:36:00 +02:00
|
|
|
const mediaItemShare = await app.$axios.$get(endpoint, { timeout: 10000 }).catch((error) => {
|
2024-06-22 23:42:13 +02:00
|
|
|
console.error('Failed', error)
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
if (!mediaItemShare) {
|
|
|
|
return error({ statusCode: 404, message: 'Not found' })
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
mediaItemShare: mediaItemShare
|
|
|
|
}
|
|
|
|
},
|
|
|
|
data() {
|
2024-06-27 00:03:12 +02:00
|
|
|
return {
|
|
|
|
localAudioPlayer: new LocalAudioPlayer(),
|
|
|
|
playerState: null,
|
2024-06-29 00:01:28 +02:00
|
|
|
playInterval: null,
|
|
|
|
hasLoaded: false,
|
2024-06-29 22:05:35 +02:00
|
|
|
totalDuration: 0,
|
|
|
|
windowWidth: 0,
|
|
|
|
windowHeight: 0,
|
|
|
|
listeningTimeSinceSync: 0
|
2024-06-27 00:03:12 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
2024-06-29 22:05:35 +02:00
|
|
|
playbackSession() {
|
|
|
|
return this.mediaItemShare.playbackSession
|
|
|
|
},
|
|
|
|
coverUrl() {
|
|
|
|
if (!this.playbackSession.coverPath) return `${this.$config.routerBasePath}/book_placeholder.jpg`
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
return `http://localhost:3333/public/share/${this.mediaItemShare.slug}/cover`
|
|
|
|
}
|
|
|
|
return `/public/share/${this.mediaItemShare.slug}/cover`
|
|
|
|
},
|
2024-06-27 00:03:12 +02:00
|
|
|
audioTracks() {
|
2024-06-29 22:05:35 +02:00
|
|
|
return (this.playbackSession.audioTracks || []).map((track) => {
|
2024-06-27 00:03:12 +02:00
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
track.contentUrl = `${process.env.serverUrl}${track.contentUrl}`
|
|
|
|
}
|
|
|
|
track.relativeContentUrl = track.contentUrl
|
|
|
|
return track
|
|
|
|
})
|
|
|
|
},
|
2024-06-29 00:01:28 +02:00
|
|
|
isPlaying() {
|
|
|
|
return this.playerState === 'PLAYING'
|
|
|
|
},
|
|
|
|
isPaused() {
|
|
|
|
return !this.isPlaying
|
|
|
|
},
|
|
|
|
chapters() {
|
2024-06-29 22:05:35 +02:00
|
|
|
return this.playbackSession.chapters || []
|
|
|
|
},
|
|
|
|
coverAspectRatio() {
|
|
|
|
const coverAspectRatio = this.playbackSession.coverAspectRatio
|
|
|
|
return coverAspectRatio === this.$constants.BookCoverAspectRatio.STANDARD ? 1.6 : 1
|
|
|
|
},
|
2024-06-29 23:15:55 +02:00
|
|
|
isMobileLandscape() {
|
|
|
|
return this.windowWidth > this.windowHeight && this.windowHeight < 450
|
|
|
|
},
|
2024-06-29 22:05:35 +02:00
|
|
|
coverWidth() {
|
|
|
|
const availableCoverWidth = Math.min(450, this.windowWidth - 32)
|
|
|
|
const availableCoverHeight = Math.min(450, this.windowHeight - 250)
|
|
|
|
|
|
|
|
const mostCoverHeight = availableCoverWidth * this.coverAspectRatio
|
|
|
|
if (mostCoverHeight > availableCoverHeight) {
|
|
|
|
return availableCoverHeight / this.coverAspectRatio
|
|
|
|
}
|
|
|
|
return availableCoverWidth
|
|
|
|
},
|
|
|
|
coverHeight() {
|
|
|
|
return this.coverWidth * this.coverAspectRatio
|
2024-06-27 00:03:12 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
playPause() {
|
2024-06-29 00:01:28 +02:00
|
|
|
if (!this.localAudioPlayer || !this.hasLoaded) return
|
2024-06-27 00:03:12 +02:00
|
|
|
this.localAudioPlayer.playPause()
|
|
|
|
},
|
2024-06-29 00:01:28 +02:00
|
|
|
jumpForward() {
|
|
|
|
if (!this.localAudioPlayer || !this.hasLoaded) return
|
|
|
|
const currentTime = this.localAudioPlayer.getCurrentTime()
|
|
|
|
const duration = this.localAudioPlayer.getDuration()
|
|
|
|
this.seek(Math.min(currentTime + 10, duration))
|
|
|
|
},
|
|
|
|
jumpBackward() {
|
|
|
|
if (!this.localAudioPlayer || !this.hasLoaded) return
|
|
|
|
const currentTime = this.localAudioPlayer.getCurrentTime()
|
|
|
|
this.seek(Math.max(currentTime - 10, 0))
|
|
|
|
},
|
|
|
|
setVolume(volume) {
|
|
|
|
if (!this.localAudioPlayer || !this.hasLoaded) return
|
|
|
|
this.localAudioPlayer.setVolume(volume)
|
|
|
|
},
|
|
|
|
setPlaybackRate(playbackRate) {
|
|
|
|
if (!this.localAudioPlayer || !this.hasLoaded) return
|
|
|
|
this.localAudioPlayer.setPlaybackRate(playbackRate)
|
|
|
|
},
|
|
|
|
seek(time) {
|
|
|
|
if (!this.localAudioPlayer || !this.hasLoaded) return
|
|
|
|
|
|
|
|
this.localAudioPlayer.seek(time, this.isPlaying)
|
|
|
|
this.setCurrentTime(time)
|
|
|
|
},
|
|
|
|
setCurrentTime(time) {
|
|
|
|
if (!this.$refs.audioPlayer) return
|
|
|
|
|
|
|
|
// Update UI
|
|
|
|
this.$refs.audioPlayer.setCurrentTime(time)
|
|
|
|
},
|
|
|
|
setDuration() {
|
|
|
|
if (!this.localAudioPlayer) return
|
|
|
|
this.totalDuration = this.localAudioPlayer.getDuration()
|
|
|
|
if (this.$refs.audioPlayer) {
|
|
|
|
this.$refs.audioPlayer.setDuration(this.totalDuration)
|
|
|
|
}
|
|
|
|
},
|
2024-06-29 22:05:35 +02:00
|
|
|
sendProgressSync(currentTime) {
|
|
|
|
console.log('Sending progress sync for time', currentTime)
|
|
|
|
const progress = {
|
|
|
|
currentTime
|
|
|
|
}
|
|
|
|
this.$axios.$patch(`/public/share/${this.mediaItemShare.slug}/progress`, progress, { progress: false }).catch((error) => {
|
|
|
|
console.error('Failed to send progress sync', error)
|
|
|
|
})
|
|
|
|
},
|
2024-06-29 00:01:28 +02:00
|
|
|
startPlayInterval() {
|
2024-06-29 22:05:35 +02:00
|
|
|
let lastTick = Date.now()
|
2024-06-29 00:01:28 +02:00
|
|
|
clearInterval(this.playInterval)
|
|
|
|
this.playInterval = setInterval(() => {
|
2024-06-29 22:05:35 +02:00
|
|
|
if (!this.localAudioPlayer) return
|
|
|
|
|
|
|
|
const currentTime = this.localAudioPlayer.getCurrentTime()
|
|
|
|
this.setCurrentTime(currentTime)
|
|
|
|
const exactTimeElapsed = (Date.now() - lastTick) / 1000
|
|
|
|
lastTick = Date.now()
|
|
|
|
this.listeningTimeSinceSync += exactTimeElapsed
|
|
|
|
if (this.listeningTimeSinceSync >= 30) {
|
|
|
|
this.listeningTimeSinceSync = 0
|
|
|
|
this.sendProgressSync(currentTime)
|
2024-06-29 00:01:28 +02:00
|
|
|
}
|
|
|
|
}, 1000)
|
|
|
|
},
|
|
|
|
stopPlayInterval() {
|
|
|
|
clearInterval(this.playInterval)
|
|
|
|
this.playInterval = null
|
|
|
|
},
|
2024-06-27 00:03:12 +02:00
|
|
|
playerStateChange(state) {
|
|
|
|
this.playerState = state
|
2024-06-29 00:01:28 +02:00
|
|
|
if (state === 'LOADED' || state === 'PLAYING') {
|
|
|
|
this.setDuration()
|
|
|
|
}
|
2024-06-27 00:03:12 +02:00
|
|
|
if (state === 'LOADED') {
|
|
|
|
this.hasLoaded = true
|
|
|
|
}
|
2024-06-29 00:01:28 +02:00
|
|
|
if (state === 'PLAYING') {
|
|
|
|
this.startPlayInterval()
|
|
|
|
} else {
|
|
|
|
this.stopPlayInterval()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
playerTimeUpdate(time) {
|
|
|
|
this.setCurrentTime(time)
|
|
|
|
},
|
|
|
|
getHotkeyName(e) {
|
|
|
|
var keyCode = e.keyCode || e.which
|
|
|
|
if (!this.$keynames[keyCode]) {
|
|
|
|
// Unused hotkey
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
var keyName = this.$keynames[keyCode]
|
|
|
|
var name = keyName
|
|
|
|
if (e.shiftKey) name = 'Shift-' + keyName
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
console.log('Hotkey command', name)
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
},
|
|
|
|
keyDown(e) {
|
|
|
|
if (!this.localAudioPlayer || !this.hasLoaded) return
|
|
|
|
|
|
|
|
var name = this.getHotkeyName(e)
|
|
|
|
if (!name) return
|
|
|
|
|
|
|
|
// Playing audiobook
|
|
|
|
if (Object.values(this.$hotkeys.AudioPlayer).includes(name)) {
|
|
|
|
this.$eventBus.$emit('player-hotkey', name)
|
|
|
|
e.preventDefault()
|
|
|
|
}
|
2024-06-29 22:05:35 +02:00
|
|
|
},
|
|
|
|
resize() {
|
|
|
|
this.windowWidth = window.innerWidth
|
|
|
|
this.windowHeight = window.innerHeight
|
2024-06-27 00:03:12 +02:00
|
|
|
}
|
2024-06-22 23:42:13 +02:00
|
|
|
},
|
|
|
|
mounted() {
|
2024-06-29 22:05:35 +02:00
|
|
|
this.resize()
|
|
|
|
window.addEventListener('resize', this.resize)
|
2024-06-29 00:01:28 +02:00
|
|
|
window.addEventListener('keydown', this.keyDown)
|
|
|
|
|
2024-06-29 22:05:35 +02:00
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
console.log('Loaded media item share', this.mediaItemShare)
|
|
|
|
}
|
|
|
|
|
|
|
|
const startTime = this.playbackSession.currentTime || 0
|
|
|
|
this.localAudioPlayer.set(null, this.audioTracks, false, startTime, false)
|
2024-06-27 00:03:12 +02:00
|
|
|
this.localAudioPlayer.on('stateChange', this.playerStateChange.bind(this))
|
2024-06-29 00:01:28 +02:00
|
|
|
this.localAudioPlayer.on('timeupdate', this.playerTimeUpdate.bind(this))
|
|
|
|
},
|
|
|
|
beforeDestroy() {
|
2024-06-29 22:05:35 +02:00
|
|
|
window.removeEventListener('resize', this.resize)
|
2024-06-29 00:01:28 +02:00
|
|
|
window.removeEventListener('keydown', this.keyDown)
|
|
|
|
|
|
|
|
this.localAudioPlayer.off('stateChange', this.playerStateChange)
|
|
|
|
this.localAudioPlayer.destroy()
|
2024-06-22 23:42:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|