repod/src/store/player.js

106 lines
2.5 KiB
JavaScript
Raw Normal View History

2023-08-29 00:47:22 +02:00
import axios from '@nextcloud/axios'
import { defineStore } from 'pinia'
import { formatEpisodeTimestamp } from '../utils/time.js'
2023-08-29 00:47:22 +02:00
import { generateUrl } from '@nextcloud/router'
const audio = new Audio()
export const usePlayer = defineStore('player', {
state: () => ({
2023-08-29 00:47:22 +02:00
currentTime: null,
duration: null,
episode: null,
loaded: false,
paused: null,
podcastUrl: null,
2023-08-29 11:43:17 +02:00
volume: 1,
rate: 1,
2024-01-16 23:13:07 +01:00
started: 0,
}),
actions: {
init: () => {
audio.ondurationchange = () => (this.duration = audio.duration)
audio.onended = () => this.stop()
audio.onloadeddata = () => (this.loaded = true)
audio.onpause = () => this.pause()
audio.onplay = () => this.play()
audio.onratechange = () => (this.rate = audio.playbackRate)
audio.onseeked = () => (this.currentTime = audio.currentTime)
audio.ontimeupdate = () => (this.currentTime = audio.currentTime)
audio.onvolumechange = () => (this.volume = audio.volume)
},
load: async (episode, podcastUrl) => {
this.episode = episode
this.podcastUrl = podcastUrl
2023-08-29 00:47:22 +02:00
if (this.episode) {
audio.src = this.episode.url
2023-08-29 00:47:22 +02:00
audio.load()
try {
const action = await axios.get(
generateUrl('/apps/repod/episodes/action?url={url}', {
url: this.episode.url,
}),
)
this.episode.action = action
} catch {}
2023-08-29 12:07:23 +02:00
if (
this.episode.action &&
this.episode.action.position &&
this.episode.action.position < this.episode.action.total
) {
audio.currentTime = this.episode.action.position
this.started = audio.currentTime
2023-08-29 12:07:23 +02:00
}
audio.play()
2023-08-29 00:47:22 +02:00
} else {
this.loaded = false
this.podcastUrl = null
2023-08-29 00:47:22 +02:00
audio.src = ''
}
},
pause: () => {
2023-08-29 00:47:22 +02:00
audio.pause()
this.paused = true
2023-08-29 00:47:22 +02:00
},
play: () => {
2024-01-16 22:36:01 +01:00
audio.play()
this.paused = false
this.started = audio.currentTime
2024-01-16 22:36:01 +01:00
},
seek: (currentTime) => {
2023-08-29 00:47:22 +02:00
audio.currentTime = currentTime
this.time()
2023-08-29 00:47:22 +02:00
},
stop: () => {
this.pause()
this.episode = null
2023-08-29 00:47:22 +02:00
},
time: () => {
this.episode.action = {
podcast: this.podcastUrl,
episode: this.episode.url,
guid: this.episode.guid,
action: 'play',
timestamp: formatEpisodeTimestamp(new Date()),
started: Math.round(this.started),
position: Math.round(audio.currentTime),
total: Math.round(audio.duration),
}
axios.post(generateUrl('/apps/gpoddersync/episode_action/create'), [
this.episode.action,
])
},
setVolume: (volume) => {
2023-08-29 00:47:22 +02:00
audio.volume = volume
},
setRate: (rate) => {
audio.playbackRate = rate
},
2023-08-29 00:47:22 +02:00
},
})