import axios from '@nextcloud/axios' import { defineStore } from 'pinia' import { formatEpisodeTimestamp } from '../utils/time.js' import { generateUrl } from '@nextcloud/router' const audio = new Audio() export const usePlayer = defineStore('player', { state: () => ({ currentTime: null, duration: null, episode: null, loaded: false, paused: null, podcastUrl: null, volume: 1, rate: 1, 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) }, async load(episode, podcastUrl) { this.episode = episode this.podcastUrl = podcastUrl if (this.episode) { audio.src = this.episode.url audio.load() try { const action = await axios.get( generateUrl('/apps/repod/episodes/action?url={url}', { url: this.episode.url, }), ) this.episode.action = action } catch {} if ( this.episode.action?.position && this.episode.action.position < this.episode.action.total ) { audio.currentTime = this.episode.action.position this.started = audio.currentTime } audio.play() } else { this.loaded = false this.podcastUrl = null audio.src = '' } }, pause() { audio.pause() this.paused = true this.time() }, play() { audio.play() this.paused = false this.started = audio.currentTime }, seek(currentTime) { audio.currentTime = currentTime this.time() }, stop() { this.pause() this.episode = null }, 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) { audio.volume = volume }, setRate(rate) { audio.playbackRate = rate }, }, })