From 8a6d6fa08920ef73002891c2ae0d0d5766311796 Mon Sep 17 00:00:00 2001 From: harold Date: Wed, 14 May 2025 20:59:43 +0500 Subject: [PATCH] add fix --- internal/model/widget-templates.go | 70 +++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/internal/model/widget-templates.go b/internal/model/widget-templates.go index 0a46938..3ec464e 100644 --- a/internal/model/widget-templates.go +++ b/internal/model/widget-templates.go @@ -106,9 +106,8 @@ function playAudio(url, volume) { const audio = new Audio(url); audio.volume = volume; audio.play().then(() => { - audio.addEventListener('ended', resolve); + audio.addEventListener('ended', () => resolve(audio)); }).catch(error => { - console.error('Error playing audio:', error); reject(error); }); }); @@ -116,7 +115,7 @@ function playAudio(url, volume) { function playSpeech(text, voiceSettings) { return new Promise((resolve, reject) => { - if (!voiceSettings.voice_enabled) return resolve(); + if (!voiceSettings.voice_enabled) return resolve(null); const requestBody = { text: text, @@ -140,31 +139,59 @@ function playSpeech(text, voiceSettings) { const url = URL.createObjectURL(blob); const audio = new Audio(url); audio.volume = (voiceSettings.voice_sound_percent || 100) / 100; - audio.play().catch(reject); - audio.addEventListener('ended', () => { - URL.revokeObjectURL(url); - resolve(); - }); + audio.play().then(() => { + audio.addEventListener('ended', () => { + URL.revokeObjectURL(url); + resolve(audio); + }); + }).catch(reject); }) .catch(reject); }); } async function playMedia(donat, voiceSettings) { + let audioElement = null; + let ttsAudio = null; + const controller = new AbortController(); + try { - let mediaPromise = Promise.resolve(); - const volume = (voiceSettings.voice_sound_percent || 100) / 100; + const timeoutPromise = new Promise(resolve => + setTimeout(() => { + controller.abort(); + resolve('timeout'); + }, donat.duration * 1000) + ); - if (donat.play_content && donat.audio_link) { - mediaPromise = playAudio(donat.audio_link, volume) - .then(() => playSpeech(donat.text, voiceSettings)); - } else if (donat.text && donat.voice_enabled) { - mediaPromise = playSpeech(donat.text, voiceSettings); + const mediaPromise = (async () => { + try { + if (donat.play_content && donat.audio_link) { + audioElement = await playAudio( + donat.audio_link, + (voiceSettings.voice_sound_percent || 100) / 100 + ); + if (donat.text && !controller.signal.aborted) { + ttsAudio = await playSpeech(donat.text, voiceSettings); + } + } else if (donat.text && donat.voice_enabled) { + ttsAudio = await playSpeech(donat.text, voiceSettings); + } + } catch (e) { + if (!controller.signal.aborted) throw e; + } + })(); + + await Promise.race([mediaPromise, timeoutPromise]); + + } finally { + if (audioElement && !audioElement.ended) { + audioElement.pause(); + audioElement.currentTime = 0; + } + if (ttsAudio && !ttsAudio.ended) { + ttsAudio.pause(); + ttsAudio.currentTime = 0; } - - await mediaPromise; - } catch (error) { - console.error('Media play error:', error); } } @@ -243,7 +270,10 @@ async function widgetView() { } const elapsed = Date.now() - iterationStart; - const remaining = 5000 - elapsed; + const remaining = donat?.duration ? + Math.max(donat.duration * 1000 - elapsed, 0) : + 5000 - elapsed; + if (remaining > 0) { await new Promise(r => setTimeout(r, remaining)); }