2023-08-29 00:47:22 +02:00
|
|
|
import axios from '@nextcloud/axios'
|
2024-08-09 00:58:25 +02:00
|
|
|
import { defineStore } from 'pinia'
|
2024-01-29 15:47:50 +01:00
|
|
|
import { formatEpisodeTimestamp } from '../utils/time.js'
|
2023-08-29 00:47:22 +02:00
|
|
|
import { generateUrl } from '@nextcloud/router'
|
|
|
|
|
|
|
|
const audio = new Audio()
|
|
|
|
|
2024-08-09 00:58:25 +02:00
|
|
|
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,
|
2024-01-16 22:12:07 +01:00
|
|
|
rate: 1,
|
2024-01-16 23:13:07 +01:00
|
|
|
started: 0,
|
2024-08-09 00:58:25 +02:00
|
|
|
}),
|
|
|
|
actions: {
|
2024-08-09 22:51:32 +02:00
|
|
|
init() {
|
2024-08-09 16:50:24 +02:00
|
|
|
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)
|
|
|
|
},
|
2024-08-09 22:51:32 +02:00
|
|
|
async load(episode, podcastUrl) {
|
2024-08-09 00:58:25 +02:00
|
|
|
this.episode = episode
|
2024-08-09 16:50:24 +02:00
|
|
|
this.podcastUrl = podcastUrl
|
2023-08-29 00:47:22 +02:00
|
|
|
|
2024-08-09 00:58:25 +02:00
|
|
|
if (this.episode) {
|
|
|
|
audio.src = this.episode.url
|
2023-08-29 00:47:22 +02:00
|
|
|
audio.load()
|
2024-08-09 00:58:25 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2024-04-30 00:48:47 +02:00
|
|
|
if (
|
2024-08-27 16:11:12 +02:00
|
|
|
this.episode.action?.position &&
|
2024-08-09 00:58:25 +02:00
|
|
|
this.episode.action.position < this.episode.action.total
|
2024-04-30 00:48:47 +02:00
|
|
|
) {
|
2024-08-09 00:58:25 +02:00
|
|
|
audio.currentTime = this.episode.action.position
|
|
|
|
this.started = audio.currentTime
|
2023-08-29 12:07:23 +02:00
|
|
|
}
|
2024-08-09 00:58:25 +02:00
|
|
|
|
|
|
|
audio.play()
|
2023-08-29 00:47:22 +02:00
|
|
|
} else {
|
2024-08-09 00:58:25 +02:00
|
|
|
this.loaded = false
|
|
|
|
this.podcastUrl = null
|
2023-08-29 00:47:22 +02:00
|
|
|
audio.src = ''
|
|
|
|
}
|
|
|
|
},
|
2024-08-09 22:51:32 +02:00
|
|
|
pause() {
|
2023-08-29 00:47:22 +02:00
|
|
|
audio.pause()
|
2024-08-09 00:58:25 +02:00
|
|
|
this.paused = true
|
2024-08-16 23:50:36 +02:00
|
|
|
this.time()
|
2023-08-29 00:47:22 +02:00
|
|
|
},
|
2024-08-09 22:51:32 +02:00
|
|
|
play() {
|
2024-01-16 22:36:01 +01:00
|
|
|
audio.play()
|
2024-08-09 00:58:25 +02:00
|
|
|
this.paused = false
|
|
|
|
this.started = audio.currentTime
|
2024-01-16 22:36:01 +01:00
|
|
|
},
|
2024-08-09 22:51:32 +02:00
|
|
|
seek(currentTime) {
|
2023-08-29 00:47:22 +02:00
|
|
|
audio.currentTime = currentTime
|
2024-08-09 00:58:25 +02:00
|
|
|
this.time()
|
2023-08-29 00:47:22 +02:00
|
|
|
},
|
2024-08-09 22:51:32 +02:00
|
|
|
stop() {
|
2024-08-09 00:58:25 +02:00
|
|
|
this.pause()
|
|
|
|
this.episode = null
|
2023-08-29 00:47:22 +02:00
|
|
|
},
|
2024-08-09 22:51:32 +02:00
|
|
|
time() {
|
2024-08-09 00:58:25 +02:00
|
|
|
this.episode.action = {
|
|
|
|
podcast: this.podcastUrl,
|
|
|
|
episode: this.episode.url,
|
|
|
|
guid: this.episode.guid,
|
2024-03-05 10:37:16 +01:00
|
|
|
action: 'play',
|
|
|
|
timestamp: formatEpisodeTimestamp(new Date()),
|
2024-08-09 00:58:25 +02:00
|
|
|
started: Math.round(this.started),
|
2024-03-05 10:37:16 +01:00
|
|
|
position: Math.round(audio.currentTime),
|
|
|
|
total: Math.round(audio.duration),
|
|
|
|
}
|
2024-04-30 00:48:47 +02:00
|
|
|
axios.post(generateUrl('/apps/gpoddersync/episode_action/create'), [
|
2024-08-09 00:58:25 +02:00
|
|
|
this.episode.action,
|
2024-04-30 00:48:47 +02:00
|
|
|
])
|
2024-03-05 10:37:16 +01:00
|
|
|
},
|
2024-08-09 22:51:32 +02:00
|
|
|
setVolume(volume) {
|
2023-08-29 00:47:22 +02:00
|
|
|
audio.volume = volume
|
|
|
|
},
|
2024-08-09 22:51:32 +02:00
|
|
|
setRate(rate) {
|
2024-01-16 22:12:07 +01:00
|
|
|
audio.playbackRate = rate
|
|
|
|
},
|
2023-08-29 00:47:22 +02:00
|
|
|
},
|
2024-08-09 00:58:25 +02:00
|
|
|
})
|