repod/src/store/player.js

107 lines
3.0 KiB
JavaScript
Raw Normal View History

2023-08-28 22:47:22 +00:00
import axios from '@nextcloud/axios'
2024-01-10 15:36:13 +00:00
import { format } from 'date-fns'
2023-08-28 22:47:22 +00:00
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,
2023-08-29 09:43:17 +00:00
volume: 1,
2023-08-28 22:47:22 +00:00
},
mutations: {
2023-08-29 06:48:54 +00:00
action: (state, action) => {
2023-08-29 10:07:23 +00:00
state.episode.action = action
2023-08-29 06:48:54 +00:00
2023-08-30 17:59:20 +00:00
if (action && action.position && action.position < action.total) {
2023-08-29 06:48:54 +00:00
audio.currentTime = action.position
}
},
2023-08-28 22:47:22 +00:00
currentTime: (state, currentTime) => {
state.currentTime = currentTime
},
duration: (state, duration) => {
state.duration = duration
},
2023-08-29 06:48:54 +00:00
episode: (state, episode) => {
2023-08-28 22:47:22 +00:00
state.episode = episode
if (episode) {
2023-12-23 21:08:26 +00:00
state.podcastUrl = atob(router.currentRoute.params.url)
2023-08-28 22:47:22 +00:00
audio.src = episode.episodeUrl
audio.load()
audio.play()
2023-08-29 10:07:23 +00:00
2023-08-30 17:59:20 +00:00
if (episode.action && episode.action.position && episode.action.position < episode.action.total) {
2023-08-29 10:07:23 +00:00
audio.currentTime = episode.action.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: {
2023-08-29 06:48:54 +00:00
load: async (context, episode) => {
context.commit('episode', episode)
2023-08-29 09:43:17 +00:00
try {
const action = await axios.get(generateUrl('/apps/repod/episodes/action?url={url}', { url: episode.episodeUrl }))
context.commit('action', action.data)
} catch {}
2023-08-29 06:48:54 +00:00
},
2023-08-28 22:47:22 +00:00
pause: (context) => {
audio.pause()
2023-12-24 10:45:37 +00:00
context.dispatch('time')
2023-08-28 22:47:22 +00:00
},
play: () => audio.play(),
seek: (context, currentTime) => {
audio.currentTime = currentTime
2023-12-24 10:45:37 +00:00
context.dispatch('time')
2023-08-28 22:47:22 +00:00
},
stop: (context) => {
context.dispatch('pause')
2023-08-29 06:48:54 +00:00
context.commit('episode', null)
context.commit('action', null)
2023-08-28 22:47:22 +00:00
},
2023-12-24 10:45:37 +00:00
time: async (context) => axios.post(generateUrl('/apps/gpoddersync/episode_action/create'), [{
podcast: context.state.podcastUrl,
episode: context.state.episode.episodeUrl,
guid: context.state.episode.episodeGuid,
action: 'play',
2024-01-10 17:21:12 +00:00
timestamp: format(new Date(), 'yyyy-MM-dd\'T\'HH:mm:ss'),
2023-12-24 10:45:37 +00:00
started: Math.round(context.state.action ? context.state.action.started : 0),
position: Math.round(audio.currentTime),
total: Math.round(audio.duration),
}]),
2023-08-28 22:47:22 +00:00
volume: (context, volume) => {
audio.volume = volume
},
},
}