repod/src/store/player.js

90 lines
2.4 KiB
JavaScript
Raw Normal View History

2023-08-28 22:47:22 +00:00
import axios from '@nextcloud/axios'
import { formatEpisodeTimestamp } from '../utils/time.js'
import { generateUrl } from '@nextcloud/router'
import router from '../router.js'
import store from './main.js'
const audio = new Audio()
audio.ondurationchange = () => store.commit('player/duration', audio.duration)
audio.onended = () => store.dispatch('player/stop')
audio.onloadeddata = () => store.commit('player/loaded', true)
audio.onplay = () => store.commit('player/paused', false)
audio.onpause = () => store.commit('player/paused', true)
audio.onseeked = () => store.commit('player/currentTime', audio.currentTime)
audio.ontimeupdate = () => store.commit('player/currentTime', audio.currentTime)
audio.onvolumechange = () => store.commit('player/volume', audio.volume)
export const player = {
namespaced: true,
state: {
currentTime: null,
duration: null,
episode: null,
loaded: false,
paused: null,
podcastUrl: null,
volume: null,
},
mutations: {
currentTime: (state, currentTime) => {
state.currentTime = currentTime
},
duration: (state, duration) => {
state.duration = duration
},
load: (state, episode) => {
state.episode = episode
if (episode) {
state.podcastUrl = router.currentRoute.params.url
audio.src = episode.episodeUrl
audio.load()
audio.play()
2023-08-28 22:51:52 +00:00
if (episode.episodeAction) {
audio.currentTime = episode.episodeAction.position
}
2023-08-28 22:47:22 +00:00
} else {
state.loaded = false
state.podcastUrl = null
audio.src = ''
}
},
loaded: (state, loaded) => {
state.loaded = loaded
},
paused: (state, paused) => {
state.paused = paused
},
volume: (state, volume) => {
state.volume = volume
},
},
actions: {
pause: (context) => {
audio.pause()
axios.post(generateUrl('/apps/gpoddersync/episode_action/create'), [{
podcast: context.state.podcastUrl,
episode: context.state.episode.episodeUrl,
guid: context.state.episode.episodeGuid,
action: 'play',
timestamp: formatEpisodeTimestamp(new Date()),
started: Math.round(context.state.episode.episodeAction ? context.state.episode.episodeAction.started : 0),
position: Math.round(audio.currentTime),
total: Math.round(audio.duration),
}])
},
play: () => audio.play(),
seek: (context, currentTime) => {
audio.currentTime = currentTime
},
stop: (context) => {
context.dispatch('pause')
context.commit('load', null)
},
volume: (context, volume) => {
audio.volume = volume
},
},
}