Fix audio control warning

This commit is contained in:
Jon Staab 2026-09-09 15:04:55 -07:00
parent dc746ca93b
commit b48f69cfa9

View file

@ -9,7 +9,7 @@
import Banner from "@app/components/Banner.svelte" import Banner from "@app/components/Banner.svelte"
import {speech, stopSpeech} from "@app/speech" import {speech, stopSpeech} from "@app/speech"
let audio: HTMLAudioElement let audio: HTMLAudioElement | undefined = $state()
let playing = $state(false) let playing = $state(false)
let currentTime = $state(0) let currentTime = $state(0)
let duration = $state(0) let duration = $state(0)
@ -18,12 +18,7 @@
// length rather than rendering a range with no end. // length rather than rendering a range with no end.
const scrubbable = $derived(Number.isFinite(duration) ? duration : 0) const scrubbable = $derived(Number.isFinite(duration) ? duration : 0)
const toggle = () => (playing ? audio.pause() : audio.play()) const toggle = () => (playing ? audio?.pause() : audio?.play())
const scrub = (event: Event & {currentTarget: HTMLInputElement}) => {
audio.currentTime = Number(event.currentTarget.value)
currentTime = audio.currentTime
}
const display = (seconds: number) => { const display = (seconds: number) => {
const whole = Math.floor(seconds) const whole = Math.floor(seconds)
@ -37,13 +32,13 @@
{#if $speech.src} {#if $speech.src}
<audio <audio
bind:this={audio} bind:this={audio}
bind:currentTime
bind:duration
src={$speech.src} src={$speech.src}
autoplay autoplay
onplay={() => (playing = true)} onplay={() => (playing = true)}
onpause={() => (playing = false)} onpause={() => (playing = false)}
onended={() => (playing = false)} onended={() => (playing = false)}></audio>
ondurationchange={() => (duration = audio.duration)}
ontimeupdate={() => (currentTime = audio.currentTime)}></audio>
<Button <Button
aria-label={playing ? "Pause message" : "Play message"} aria-label={playing ? "Pause message" : "Play message"}
class="button button-circle button-sm button-primary shrink-0" class="button button-circle button-sm button-primary shrink-0"
@ -68,8 +63,7 @@
min="0" min="0"
max={scrubbable} max={scrubbable}
step="0.1" step="0.1"
value={currentTime} bind:value={currentTime} />
oninput={scrub} />
<span class="whitespace-nowrap">{display(currentTime)} / {display(scrubbable)}</span> <span class="whitespace-nowrap">{display(currentTime)} / {display(scrubbable)}</span>
</span> </span>
{/if} {/if}