如何向我的Javascript文本到语音添加暂停和停止功能

时间:2020-11-03 12:30:09

标签: javascript text-to-speech

我似乎无法听懂文字,想停下来停下来,我想知道是否有人知道。

我的简单文本转语音功能

function TextToSpeech()
{
   const speech = new SpeechSynthesisUtterance();
    let voices = speechSynthesis.getVoices();
    let convert = document.getElementById("text").innerHTML;

    speech.text = convert;
   
    speech.volume = 1;
    speech.rate = 1;
    speech.pitch = 1;
   
    speech.voice = voices[1];
   
    window.speechSynthesis.speak(speech);
}


function Pause()
{
    speech.Pause;
}


function Stop()
{
    speech.Stop;
}

1 个答案:

答案 0 :(得分:0)

为了扩展我的评论,我已经更改并添加到您的代码中以显示方法的工作原理。

function textToSpeech()
{
   const speech = new SpeechSynthesisUtterance();
    let voices = speechSynthesis.getVoices();
    let convert = document.getElementById("text").innerHTML;

    speech.text = convert;
   
    speech.volume = 1;
    speech.rate = 1;
    speech.pitch = 1;
   
    speech.voice = voices[1];
   
    speechSynthesis.speak(speech);
}


function pause()
{
    speechSynthesis.pause();
}


function stop()
{
    speechSynthesis.cancel();
}

speakBtn.addEventListener('click', textToSpeech);
pauseBtn.addEventListener('click', pause);
cancelBtn.addEventListener('click', stop);
<input type="button" id="speakBtn" value="Speak">
<input type="button" id="pauseBtn" value="Pause">
<input type="button" id="cancelBtn" value="Cancel">
<p id="text">Text to speak goes here</p>

相关问题