voiceschanged 事件未在 Safari 中触发 [英] voiceschanged event not fired in Safari

查看:126
本文介绍了voiceschanged 事件未在 Safari 中触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,无论是 Mac 还是 iOS,Safari 都不会触发 voiceschanged 事件.同样奇怪的是,它似乎也不会在 iOS 上的 Chrome 中触发,但我假设 iOS 上的 Chrome 使用与 Safari 相同的 JavaScript 引擎.

As far as I can tell, the voiceschanged event doesn't fire in Safari, either on macs or in iOS. Also odd, it doesn't seem to fire in Chrome on iOS either, but I'm assuming Chrome on iOS uses the same JavaScript engine as Safari.

这是我用来验证的演示:http://jsbin.com/gosaqihi/9/edit?js,console(取自 获取Chrome(Web Speech API)的speechSynthesis中的语音列表)

Here's a demonstration that I used to verify: http://jsbin.com/gosaqihi/9/edit?js,console (taken from Getting the list of voices in speechSynthesis of Chrome (Web Speech API))

我也使用 addEventListener 尝试过:

I've also tried it using addEventListener:

speechSynthesis.addEventListener("voiceschanged", function () {
    var voices = speechSynthesis.getVoices(),
        keys = Object.keys(voices[0]),
        i, j;

    document.write("<table border=1><tr>");

    for ( i = 0; i < keys.length; i++ ) {
        document.write("<td>" + keys[i] + "</td>");
    }

    document.write("</tr>");

    for ( i = 0; i < voices.length; i++ ) {
        document.write("</tr>");
        for ( j = 0; j < keys.length; j++ ) {
            document.write("<td>" + voices[i][keys[j]] + "</td>");
        }
        document.write("</tr>");
    }

    document.write("<table>");
}, false);

这两种方法(onvoiceschanged、addEventListener)在适用于 Windows、Android 和 Mac 的 Chrome 中都可以正常工作,但在适用于 iOS 的 Chrome 和适用于 Mac 和 iOS 的 Safari 中失败.据我所知,Safari 根本不会触发 voiceschanged 事件.

Both approaches (onvoiceschanged, addEventListener) work fine in Chrome for Windows, Android, and Mac, but fail on Chrome for iOS and Safari for Mac and iOS. As far as I can tell, Safari simply doesn't fire the voiceschanged event.

让事情复杂化,我实际上没有任何 Apple 设备,所以我不得不通过让朋友尝试来解决这个问题.

Complicating things, I don't actually own any Apple devices, so I've had to figure this out by having friends try things.

我需要在 Safari 中做些什么来获取声音列表吗?还是语音合成 API 还没有(完全)实现?

Is there something special I need to do in Safari to get the list of voices? Or is the Speech Synthesis API simply not (fully) implemented yet?

推荐答案

显然 到目前为止,Safari 仅部分支持 Web Speech API.

Apparently Safari only has partial support for the Web Speech API so far.

为了让您的代码在不同环境中工作,您可以做的是检测 speechSynthesis 中是否存在 onvoiceschanged.如果没有,您可以只调用 speechSynthesis.getVoices() 而无需监听 onvoiceschanged.

What you could do for your code to work in different environments is to detect if onvoiceschanged exists in speechSynthesis. If not, you can just call speechSynthesis.getVoices() without listening for onvoiceschanged.

function doVoices() {
    var voices = speechSynthesis.getVoices(),
    // ...
}

if ('onvoiceschanged' in speechSynthesis) {
    speechSynthesis.onvoiceschanged = doVoices;
} else {
    doVoices();
}

这篇关于voiceschanged 事件未在 Safari 中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆