From 4b6cd1f41eb12e900e7434496f218f2123399620 Mon Sep 17 00:00:00 2001 From: boyska Date: Sun, 19 Mar 2023 12:57:58 +0000 Subject: [PATCH] Player: track loading state better --- src/components/Player.vue | 40 ++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/components/Player.vue b/src/components/Player.vue index 053bf93..67d1312 100644 --- a/src/components/Player.vue +++ b/src/components/Player.vue @@ -1,8 +1,9 @@ @@ -28,22 +29,18 @@ export default { eventBus.$off('play:now') }, mounted () { -// XXX: should track events from this.player, instead of manually setting const t = this eventBus.$on('play:now', function (url, metadata) { t.play(url, metadata) - t.status = 'playing' }) eventBus.$on('play:pause', function() { if(t.player) { t.player.pause() - t.status = 'pause' } }) eventBus.$on('play:resume', function() { if(t.player) { t.player.play() - t.status = 'playing' } }) }, @@ -54,23 +51,36 @@ export default { onPause() { eventBus.$emit('play:pause') }, + updateStatusFromPlayer () { + this.status = this.player.playing() ? 'playing' : 'pause' + }, play (url, metadata) { + this.loading = true let newPlayer = new Howl({ autoSuspend: false, html5: true, preload: true, src: [url], }) + const t = this + for(const eventName of ['pause', 'play', 'end']) { + newPlayer.on(eventName, this.updateStatusFromPlayer) + } + newPlayer.once('play', function setProperData() { + if(t.player) { + t.player.stop() + t.player.unload() + } + t.player = newPlayer + if(metadata !== undefined) { + t.title = metadata.title + t.live = metadata.live + } + t.loading = false + t.updateStatusFromPlayer() + }) newPlayer.play() - if(this.player) { - this.player.stop() - } - this.player = newPlayer - if(metadata !== undefined) { - this.title = metadata.title - this.live = metadata.live - } }, stop () { if (this.player) {