【问题标题】:Trying to use speechSynthesis API in iOS webview app尝试在 iOS webview 应用程序中使用 SpeechSynthesis API
【发布时间】:2018-10-11 21:56:33
【问题描述】:

所以我正在尝试创建一个可以进行 TTS 的应用。

我在 webview 应用程序中使用 SpeechSynthesis API(React 前端,使用 Cordova 编译,但我愿意切换到 React Native)

我有一个相当标准的实现,它适用于所有浏览器,包括 Safari - 声音甚至可以通过 Xcode 在 iOS 模拟器中运行。但是,当我尝试在我的设备上运行它时,没有声音工作,即使我与 SpeechAPI 关联的其余代码(主要是文本突出显示)按预期工作。

        function speechAPI(text, chardivs, speed, volume, version) {
            if (speechSynthesis.paused) {
                speechSynthesis.resume();
                return;
            }
            let msg = new SpeechSynthesisUtterance();
            let voices = window.speechSynthesis.getVoices();
            msg.voiceURI = 'native';
            msg.volume = volume; // 0 to 1
            msg.rate = speed; // 0.1 to 10
            msg.pitch = 1; //0 to 2
            msg.text = text;
            msg.lang = 'zh-CN';
            msg.voice = voices[63];
...

我没有看到任何错误或类似的东西。我是否缺少一些设置来使此 API 通过 iOS 工作?我的设备运行的是 iOS 11,如果这提供了任何有用的信息。

【问题讨论】:

标签: javascript ios reactjs speech-synthesis


【解决方案1】:

如果我没记错的话,我了解到您在调用 SpeechSynthesis.speak() 方法时无法让 iOS 设备真正说话。

我不完全确定 WebView,但 Safari(WebView 可能基于它)需要在用户操作上至少触发一次 SpeechSynthesis.speak()。 例如。

//This is the button a user has to click. after this event has been triggered,
//the speechSynthesis.speak() can be used autonomously as well.
document.getElementById('enable_voice_output').addEventListener('click', primeSpeak);
var speechPrimed = false;
function primeSpeak(){
  if(speechPrimed === false){
    var u = new SpeechSynthesisUtterance("Speech is enabled");
    speechSynthesis.speak(u);
    speechPrimed = true;
  }
}

【讨论】:

  • 你是对的。我们可以将音量设置为 0,这样用户就不会注意到了。
猜你喜欢
  • 2014-05-17
  • 2018-09-07
  • 1970-01-01
  • 1970-01-01
  • 2020-05-11
  • 2022-10-18
  • 1970-01-01
  • 1970-01-01
  • 2014-09-27
相关资源
最近更新 更多