From 89f642cea91c25b6b138af24a81044117a961d92 Mon Sep 17 00:00:00 2001 From: mm Date: Wed, 30 Oct 2024 16:22:07 +0500 Subject: [PATCH] dev --- internal/model/widget-templates.go | 58 ++++++++++++++++-------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/internal/model/widget-templates.go b/internal/model/widget-templates.go index 4dabb62..6b939bf 100644 --- a/internal/model/widget-templates.go +++ b/internal/model/widget-templates.go @@ -68,31 +68,32 @@ function addText(text) { contentDiv.appendChild(p); } -function playAudio(audioUrl, widgetID) { +function playAudio(audioUrl, widgetID, callback) { if (!audioUrl.includes("http")) { - audioUrl = "http://147.45.237.174/api/widget/media/audio/file/get/" + String(widgetID) + audioUrl = "http://147.45.237.174/api/widget/media/audio/file/get/" + String(widgetID); } fetch(audioUrl) - .then(response => { - if (!response.ok) { - throw new Error('Ошибка сети: ' + response.statusText); - } - return response.blob(); - }) - .then(blob => { - const url = URL.createObjectURL(blob); - const audio = new Audio(url); + .then(response => { + if (!response.ok) { + throw new Error('Ошибка сети: ' + response.statusText); + } + return response.blob(); + }) + .then(blob => { + const url = URL.createObjectURL(blob); + const audio = new Audio(url); - audio.play().catch(error => { - console.error('Ошибка при воспроизведении:', error); + audio.play().catch(error => { + console.error('Ошибка при воспроизведении:', error); + }); + + audio.addEventListener('ended', () => { + audio.pause(); + audio.remove(); + console.log('Музыка закончила играть'); + if (callback) callback(); // Вызовите callback после завершения аудио + }); }); - - setTimeout(() => { - audio.pause(); - audio.remove() - console.log('Воспроизведение остановлено'); - }, 5000); - }) } function playSpeech(text) { @@ -128,6 +129,10 @@ function playSpeech(text) { ) } +function playSpeechAfterAudio(text) { + playAudio(widget.AudioUrl, widgetID, () => playSpeech(text)); +} + function delay(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } @@ -167,14 +172,13 @@ async function widgetView() { if (widget.ImageUrl != "") { addImage(widget.ImageUrl) } - if (widget.AudioUrl) { - playAudio(widget.AudioUrl, widgetID) - } - await delay(6000); - playSpeech(donat.Text) - addText(donat.Text + " " + donat.Amount) - + + if (widget.AudioUrl) { + playSpeechAfterAudio(donat.Text); + } else { + playSpeech(donat.Text); // Если нет аудио, просто сразу играет речь + } await delay(widget.Duration * 1000); await endDonat(donat.ID) }