From 8803550eb95ef2154bd2be6860ddf2c3f536fd62 Mon Sep 17 00:00:00 2001 From: Megghy Date: Sun, 17 Dec 2023 23:20:54 +0800 Subject: [PATCH] fix tts --- src/views/open_live/ReadDanmaku.vue | 172 +++++++++++++++++----------- 1 file changed, 105 insertions(+), 67 deletions(-) diff --git a/src/views/open_live/ReadDanmaku.vue b/src/views/open_live/ReadDanmaku.vue index fb84306..537c948 100644 --- a/src/views/open_live/ReadDanmaku.vue +++ b/src/views/open_live/ReadDanmaku.vue @@ -132,7 +132,7 @@ async function speak() { speechCount.value-- readedDanmaku.value++ console.log(`[TTS] 正在朗读: ${text}`) - await EasySpeech.speak({ + /*await EasySpeech.speak({ text: text, volume: settings.value.speechInfo.volume, pitch: settings.value.speechInfo.pitch, @@ -150,7 +150,29 @@ async function speak() { }) .finally(() => { isSpeaking.value = false - }) + })*/ + const synth = window.speechSynthesis + let u = new SpeechSynthesisUtterance() + u.text = text + let voices = synth.getVoices() + const voice = voices.find((v) => v.name === settings.value.speechInfo.voice) + if (voice) { + u.voice = voice + u.volume = settings.value.speechInfo.volume + u.rate = settings.value.speechInfo.rate + u.pitch = settings.value.speechInfo.pitch + synth.speak(u) + u.onend = () => { + isSpeaking.value = false + } + u.onerror = (err) => { + if (err.error == 'interrupted') { + return + } + console.log(err) + message.error('无法播放语音: ' + err.error) + } + } } } function onGetEvent(data: EventModel) { @@ -219,6 +241,7 @@ function stopSpeech() { } function cancelSpeech() { EasySpeech.cancel() + isSpeaking.value = false } async function uploadConfig() { await QueryPostAPI(VTSURU_API_URL + 'set-config', { @@ -351,77 +374,92 @@ onUnmounted(() => {