简体   繁体   English

speechSynthesis.pause() 应该返回 true 但返回 false

[英]speechSynthesis.pause() should return true but returning false

When I play and pause and play again it does not play because当我播放并暂停并再次播放时,它不会播放,因为

speechSynthesis.pause() does not make speechSynthesis.paused true which should make it true. speechSynthesis.pause() 不会使 speechSynthesis.paused 为真,这应该使它为真。

I've checked the docs it says我检查了它说的文档

The paused read-only property of the SpeechSynthesis interface is a Boolean that returns true if the SpeechSynthesis object is in a paused state, or false if not. SpeechSynthesis 接口的暂停只读属性是一个 Boolean,如果 SpeechSynthesis object 处于暂停状态 state 则返回 true,否则返回 false。

meaning the line speechSynthesis.pause() didnt set it to true意思是speechSynthesis.pause()行没有将其设置为 true

Sample below:示例如下:

 const playButton = document.getElementById("play-button"); const pauseButton = document.getElementById("pause-button"); const stopButton = document.getElementById("stop-button"); const textInput = document.getElementById("text"); const speedInput = document.getElementById("speed"); const status = document.getElementById("status"); let currentCharacter; const utterance = new SpeechSynthesisUtterance(); utterance.addEventListener("end", () => { textInput.disabled = false; }) utterance.addEventListener("bounary", e => { currentCharacter = e.charIndex; }) playButton.addEventListener("click", () => { playText(textInput.value); }) pauseButton.addEventListener("click", pauseText); stopButton.addEventListener("click", stopText); speedInput.addEventListener("input", () => { stopText(); playText(utterance.text.substring(currentCharacter)) }) function playText(text) { if (speechSynthesis.paused && speechSynthesis.speaking) { status.innerHTML = "resumed"; return speechSynthesis.resume() } status.innerHTML = "played"; if (speechSynthesis.speaking) return utterance.text = text; utterance.rate = speedInput.value || 1; textInput.disabled = true; speechSynthesis.speak(utterance); } function pauseText() { if (speechSynthesis.speaking) { status.innerHTML = "paused"; speechSynthesis.pause() // } } function stopText() { status.innerHTML = "stopped"; speechSynthesis.resume() speechSynthesis.cancel() }
 body { width: 90%; margin: 0 auto; margin-top: 1rem; } #text { width: 100%; height: 50vh; }
 <textarea name="text" id="text"></textarea> <label for="speed">Speed</label> <input type="number" name="speed" id="speed" min=".5" max="3" step=".5" value="1"> <button id="play-button">Play</button> <button id="pause-button">Pause</button> <button id="stop-button">Stop</button> <span id="status"></span>

You can use this code to see the statuses upon clicking Play, Pause and Stop.您可以使用此代码查看单击播放、暂停和停止后的状态。 The statues are then updated when the relevent event is triggered.然后在触发相关事件时更新雕像。 Ie when you click Play the statuses are shown once upon clicking and again upon the onstart event being fired.即,当您单击“播放”时,状态会在单击时显示一次,然后在触发 onstart 事件时显示。

You'll see that there is a difference between these two for example when you click Pause the paused flag is false until the onpause event is triggered a moment later.您会看到这两者之间存在差异,例如,当您单击暂停时,暂停标志为假,直到稍后触发 onpause 事件。

Given that chrome has problems and there are timing issues I suggest that you keep it simple and have start, pause, resume and cancel buttons with their own functions.鉴于 chrome 存在问题并且存在时间问题,我建议您保持简单,并拥有具有自己功能的开始、暂停、恢复和取消按钮。

 const showStatus = (btn) => { const statuses = document.createTextNode(` ${btn}: Pending: ${speechSynthesis.pending}, Speaking: ${speechSynthesis.speaking}, Paused: ${speechSynthesis.paused}`); const p = document.createElement('p'); p.appendChild(statuses); data.insertBefore(p, data.firstElementChild); }; play.addEventListener("click", (e) => { const u = new SpeechSynthesisUtterance( "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15" ); speechSynthesis.speak(u); showStatus(e.target.innerText); u.onstart = () => showStatus('On start'); u.onpause = () => showStatus('On pause'); u.onresume = () => showStatus('On resume'); u.onend = () => showStatus('On end'); u.onerror = (err) => showStatus('On error ' + err); }); pause.addEventListener("click", (e) => { speechSynthesis.pause(); showStatus(e.target.innerText); }); resume.addEventListener("click", (e) => { speechSynthesis.resume(); showStatus(e.target.innerText); }); ssCancel.addEventListener('click', (e) => { speechSynthesis.cancel(); showStatus(e.target.innerText); }); showStatus('Upon load');
 <button id="play">Play</button> <button id="pause">Pause</button> <button id="resume">Resume</button> <button id="ssCancel">Cancel</button> <div id="data"></div>

I had the same problem.我有同样的问题。 My reading of the docs did not coincide with what I experienced.我对文档的阅读与我的经历不一致。 I ended up simply setting my own variable to keep track of whether there is a pause.我最终只是简单地设置了自己的变量来跟踪是否有暂停。 Of note, the speaking property can be used to find if the utterance has not been started, or has finished.值得注意的是,speaking 属性可用于查找话语是否尚未开始或已经结束。 That coupled with my own variable keeping track of what to do when the user presses the pause button allowed me to get a stop, play and pause feature.再加上我自己的变量来跟踪用户按下暂停按钮时要做什么,这使我能够获得停止、播放和暂停功能。

Edit: This works for me in Edge Browser.编辑:这在 Edge 浏览器中对我有用。 I plan to update after I try in others.我计划在尝试其他人后进行更新。

 const synth = window.speechSynthesis; let myPauseProperty = false; let utterThis; function stop() { myPauseProperty = false; synth.cancel(utterThis); } function play() { if ((myPauseProperty === true)&&(synth.speaking===true)) {//alredy started not ended //speech somewhere in the middle, and paused, needs resumed synth.resume(); myPauseProperty=false; } else if (synth.speaking===false) { //not started or has ended, user expects to start play utterThis = new SpeechSynthesisUtterance(document.getElementById("text-area").value); synth.speak(utterThis); myPauseProperty=false; } } function pause() { if ((myPauseProperty === true)&&(synth.speaking===true)) { //speech somewhere in middle of phrase and paused, user wants unpause synth.resume(); myPauseProperty = false; } else if ((myPauseProperty === false)&&(synth.speaking===true)) { //speech somewhere in middle of phrase and unpaused, user wants pause synth.pause() myPauseProperty=true; } }
 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width. initial-scale=1.0"> <title>Document</title> </head> <body> <textarea id="text-area">Put your text here and try playing;</textarea> <nav> <button onclick="stop();">[]</button> <button onclick="play();">|></button> <button onclick="pause();">||</button> </nav> </body> </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM