From c7e0d93fb0493da1001e822161083e3daa3b39b2 Mon Sep 17 00:00:00 2001 From: Neshura Date: Mon, 22 Apr 2024 20:06:25 +0200 Subject: [PATCH] Add Playback Modes --- src/routes/+page.svelte | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 01e802d..03bba3b 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -11,9 +11,43 @@ //let audioSource: HTMLAudioElement = $state(new Audio()); //let paused: boolean = $derived(audioSource.paused); //let volume: number = $derived(audioSource.volume); + enum PlaybackMode { + Linear, + LoopOne, + LoopQueue, + } + class PlaybackState { + values = { + [PlaybackMode.Linear]: "1", + [PlaybackMode.LoopOne]: "0", + [PlaybackMode.LoopQueue]: "00", + } + + current: PlaybackMode = $state(PlaybackMode.Linear); + + next = function() { + console.log(this.current) + if (this.current == PlaybackMode.LoopQueue) { + this.current = -1; + } + return this.values[++this.current]; + } + + prev = function() { + if (this.current == PlaybackMode.Linear) { + this.current = this.values.length; + } + return this.values[--this.current]; + } + + get = function() { + return this.values[this.current]; + } + } let source: HTMLAudioElement = $state(); + let mode = $state(new PlaybackState()); let title = $state(""); let queueIndex = $state(0); let currentSong = $state({ @@ -119,7 +153,11 @@ progress = source.currentTime; } - source.load(); + currentSong.source.onended = () => { + if (mode.current == PlaybackMode.LoopOne) { + currentSong.source.play(); + } + } } function play() { @@ -199,6 +237,7 @@ {:else} {/if} +