如何为我的 Javascript Text to Speech 添加暂停和停止功能
How to add a Pause and Stop function to my 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;
}
为了扩展我的评论,我已经更改并添加到您的代码中以展示这些方法的工作原理。
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>
我似乎无法将文本转为语音,暂停和停止,我想知道是否有人知道。
我的简单文字转语音功能
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;
}
为了扩展我的评论,我已经更改并添加到您的代码中以展示这些方法的工作原理。
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>