From 94dc6e2fcdfc4fa4154922e1fd65bddcd4768fb1 Mon Sep 17 00:00:00 2001 From: Neshura Date: Sun, 21 Apr 2024 23:39:56 +0200 Subject: [PATCH] Cleaned Up Controls Code --- src/lib/components/ui/separator/index.ts | 7 + .../components/ui/separator/separator.svelte | 22 +++ src/routes/+page.svelte | 141 +++++++++--------- 3 files changed, 99 insertions(+), 71 deletions(-) create mode 100644 src/lib/components/ui/separator/index.ts create mode 100644 src/lib/components/ui/separator/separator.svelte diff --git a/src/lib/components/ui/separator/index.ts b/src/lib/components/ui/separator/index.ts new file mode 100644 index 0000000..82442d2 --- /dev/null +++ b/src/lib/components/ui/separator/index.ts @@ -0,0 +1,7 @@ +import Root from "./separator.svelte"; + +export { + Root, + // + Root as Separator, +}; diff --git a/src/lib/components/ui/separator/separator.svelte b/src/lib/components/ui/separator/separator.svelte new file mode 100644 index 0000000..be3843a --- /dev/null +++ b/src/lib/components/ui/separator/separator.svelte @@ -0,0 +1,22 @@ + + + diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 993c4b4..1d65a6e 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -4,7 +4,9 @@ import {OpenSubsonic} from "$lib/opensubsonic"; import {onMount} from "svelte"; import QueueFrame from "$lib/components/custom/QueueFrame.svelte"; - import PlayerFrame from "$lib/components/custom/PlayerFrame.svelte"; + //import PlayerFrame from "$lib/components/custom/PlayerFrame.svelte"; + import {Button} from "$lib/components/ui/button"; + import {browser} from "$app/environment"; //let audioSource: HTMLAudioElement = $state(new Audio()); //let paused: boolean = $derived(audioSource.paused); @@ -12,47 +14,10 @@ let source: HTMLAudioElement = $state(); - - let audio = $state({ - get paused() { - if (source) { - return source.paused; - } - else { - return true; - } - }, - - get volume() { - if (source) { - return source.volume; - } - else { - return 0; - } - }, - set volume(volume) { - source.volume = volume; - }, - - get duration() { - if (source) { - return source.duration; - } - else { - return 0; - } - }, - - get currentTime() { - if (source) { - return source.currentTime; - } - else { - return 0; - } - } - }); + let isPaused = $state(true); + let volume = $state(0.2); + let progress = $state(0); + let duration = $state(0); let queue: Array = $state([]); @@ -89,7 +54,7 @@ } } - async function playSong(songIndex: number) { + function playSong(songIndex: number) { console.log(queue[songIndex].title); const chosenSong = queue[songIndex]; let parameters = [ @@ -103,28 +68,62 @@ ]; let url = OpenSubsonic.getApiUrl("stream", parameters); - await pause(); - source = new Audio(url); - await play(); + pause(); + newSong(url); + play(); } - async function play() { - await source.play().catch(() => {}); - await new Promise(resolve => {setTimeout(resolve, 50)}); - audio.volume = 0.2; - forceUpdate(); + function newSong(url: string) { + source = new Audio(url); // Assign new URL + + // Reassign Event Handlers + source.onloadedmetadata = () => { + duration = source.duration; + }; + + source.onplay = () => { + source.volume = volume; + isPaused = source.paused; + } + + source.onpause = () => { + isPaused = source.paused; + } + + source.ontimeupdate = () => { + progress = source.currentTime; + } } - async function pause() { + + function play() { + source.play().catch(() => {}); + } + function pause() { source.pause(); - await new Promise(resolve => {setTimeout(resolve, 50)}); - forceUpdate(); - //audio.paused = audio.source.paused; } - function forceUpdate() { - let tmp = audio; - audio = {}; - audio = tmp; + $effect(() => { + if (source) { + source.volume = volume; + } + }); + + function progressPercent() { + return progress / duration * 100 || 0; + } + + function displayTime(rawSeconds: number) { + const intSeconds = rawSeconds.toFixed(0); + const seconds = intSeconds % 60; + const minutes = (intSeconds / 60).toFixed(0) % 60; + const hours = (intSeconds / 360).toFixed(0); + + if (hours == 0) { + return `${minutes.toString()}:${seconds.toString().padStart(2, 0)}` + } + else { + return `${hours}:${minutes.toString().padStart(2, 0)}:${seconds.toString().padStart(2, 0)}` + } } onMount(() => { @@ -139,21 +138,21 @@

Center

- - - {#if typeof audio !== "undefined"} -

{audio.currentTime}

- {/if} + +
- {#if typeof audio !== "undefined"} - - {/if} +
+

Volume: {volume}

+

{displayTime(progress)}/{displayTime(duration)}

+

{progressPercent().toFixed(2)}%

+ {#if isPaused} + + {:else} + + {/if} +