From ae79dcaeca0491b4f39ff697508d1f30c3b548dc Mon Sep 17 00:00:00 2001 From: pillar Date: Sat, 14 Oct 2023 15:13:36 -0700 Subject: [PATCH] fix: update lazy audio --- src/lib/components/speech.svelte | 49 +++++++++++++++------------ src/lib/helpers/loop-play-hooks.ts | 24 ++++++------- src/routes/article/learn/+page.svelte | 5 ++- 3 files changed, 41 insertions(+), 37 deletions(-) diff --git a/src/lib/components/speech.svelte b/src/lib/components/speech.svelte index a0a5305..542ee45 100644 --- a/src/lib/components/speech.svelte +++ b/src/lib/components/speech.svelte @@ -11,17 +11,7 @@ const handleSpeech = async (e: { currentTarget: HTMLElement }, connectNext?: boolean) => { const button = e.currentTarget; - const nowAudio = button.querySelector('audio'); - if (nowAudio && !connectNext) { - if (speechCache.lastAudio === nowAudio) { - nowAudio.currentTime = 0.05; - nowAudio.pause(); - speechCache.lastAudio = null; - return; - } - speechCache.lastAudio = nowAudio; - } if (text) { audioLoaded = { ...audioLoaded, @@ -32,7 +22,19 @@ }).toString()}`, }; await tick(); - nowAudio?.load(); + } + + const nowAudio = button.querySelector('audio'); + nowAudio?.load(); + + if (nowAudio && !connectNext) { + if (speechCache.lastAudio === nowAudio) { + nowAudio.currentTime = 0.05; + nowAudio.pause(); + speechCache.lastAudio = null; + return; + } + speechCache.lastAudio = nowAudio; } document.querySelectorAll('audio').forEach((audio) => { @@ -51,11 +53,11 @@ } }; - let audio: HTMLAudioElement; + let span: HTMLSpanElement; onMount(() => { - if (connect >= 0 && audio) { + if (connect >= 0 && span) { /* eslint-disable @typescript-eslint/no-explicit-any */ - (audio as any).nowConnect = connect; + (span as any).nowConnect = connect; // (audio as any).handleSpeech = handleSpeech; } }); @@ -63,13 +65,16 @@ - handleSpeech(e)} class={className}> -