put player logic into service

This commit is contained in:
Jonas Heinrich 2021-01-19 22:34:48 +01:00
parent bff3abd16c
commit f102d87456
2 changed files with 12 additions and 66 deletions

View File

@ -28,7 +28,7 @@ export default ({
isPlaying: false, isPlaying: false,
isBuffering: false, isBuffering: false,
isMute: false, isMute: false,
isPaused: false, isPausing: false,
volume: localStorage.getItem('radio.volume') || 0.5, volume: localStorage.getItem('radio.volume') || 0.5,
oldVolume: localStorage.getItem('radio.volume') || 0.5, oldVolume: localStorage.getItem('radio.volume') || 0.5,
title: '', title: '',
@ -37,6 +37,9 @@ export default ({
setPlaying(state, playerState) { setPlaying(state, playerState) {
state.isPlaying = playerState state.isPlaying = playerState
}, },
setPausing(state, pauseState) {
state.isPausing = pauseState
},
setBuffering(state, bufferingState) { setBuffering(state, bufferingState) {
state.isBuffering = bufferingState state.isBuffering = bufferingState
}, },
@ -87,16 +90,10 @@ export default ({
context.commit('setTitle', title) context.commit('setTitle', title)
}, },
playStation(context, station) { playStation(context, station) {
if (context.state.isPaused) { context.commit('setBuffering', true)
context.commit('setPausing', false) context.commit('setTitle', station.name)
context.commit('setBuffering', true) context.commit('setPausing', false)
player.doResume() player.doPlay(station.url_resolved)
} else {
context.commit('setBuffering', true)
context.commit('setTitle', station.name)
context.commit('setPausing', false)
player.doPlay(station.enclosure)
}
}, },
}, },

View File

@ -59,17 +59,12 @@ import EmptyContent from '@nextcloud/vue/dist/Components/EmptyContent'
import Navigation from './../components/Navigation' import Navigation from './../components/Navigation'
import Table from './../components/Table' import Table from './../components/Table'
import Sidebar from './../components/Sidebar' import Sidebar from './../components/Sidebar'
import { Howl, Howler } from 'howler'
import { showError } from '@nextcloud/dialogs'
import Vuex from 'vuex' import Vuex from 'vuex'
import { RadioBrowserApi } from './../services/RadioBrowserApi' import { RadioBrowserApi } from './../services/RadioBrowserApi'
const apiClient = new RadioBrowserApi() const apiClient = new RadioBrowserApi()
let audioPlayer = null
export default { export default {
name: 'Main', name: 'Main',
components: { components: {
@ -138,18 +133,6 @@ export default {
}, },
watch: { watch: {
$route: 'onRoute', $route: 'onRoute',
'player.volume'(newVolume, oldVolume) {
if (audioPlayer !== null) {
audioPlayer.volume(newVolume)
}
},
'player.isPaused'(newState, oldState) {
if (newState === true && audioPlayer !== null) {
audioPlayer.pause()
} else if (newState === false && audioPlayer !== null) {
audioPlayer.play()
}
},
}, },
mounted() { mounted() {
this.onRoute() this.onRoute()
@ -193,44 +176,10 @@ export default {
* Start playing a radio station and counting the playback * Start playing a radio station and counting the playback
* @param {Object} station Station object * @param {Object} station Station object
*/ */
async doPlay(station) { doPlay(station) {
const vm = this
vm.$store.dispatch('isBuffering', true) /* Start streaming station */
this.$store.dispatch('playStation', station)
if (audioPlayer !== null) {
audioPlayer.fade(vm.player.volume, 0, 500)
}
vm.$store.dispatch('setTitle', station.name)
let stationSrc = ''
if (!station.url_resolved) {
stationSrc = station.urlresolved
} else {
stationSrc = station.url_resolved
}
Howler.unload()
audioPlayer = new Howl({
src: stationSrc,
html5: true,
volume: vm.player.volume,
onplay() {
vm.$store.dispatch('isPlaying', true)
vm.$store.dispatch('isBuffering', false)
},
onpause() {
vm.$store.dispatch('isPlaying', false)
vm.$store.dispatch('isBuffering', false)
},
onend() {
showError(t('radio', 'Lost connection to radio station, retrying ...'))
vm.$store.dispatch('isPlaying', false)
vm.$store.dispatch('isBuffering', true)
},
})
audioPlayer.unload()
audioPlayer.play()
audioPlayer.fade(0, vm.player.volume, 500)
/* Count click */ /* Count click */
apiClient.countClick(station) apiClient.countClick(station)