repod/src/store/player.js

106 lines
2.9 KiB
JavaScript
Raw Normal View History

2023-08-28 22:47:22 +00:00
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
import moment from '@nextcloud/moment'
2023-08-28 22:47:22 +00:00
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)
audio.src = episode.url
2023-08-28 22:47:22 +00:00
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.url }))
2023-08-29 09:43:17 +00:00
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)
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.url,
guid: context.state.episode.guid,
2023-12-24 10:45:37 +00:00
action: 'play',
timestamp: moment().format('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),
}]),
volume: (_, volume) => {
2023-08-28 22:47:22 +00:00
audio.volume = volume
},
},
}