【问题标题】:Text-to-speech doesn't work in an Android WebView and Firefox文本转语音在 Android WebView 和 Firefox 中不起作用
【发布时间】:2018-01-02 20:05:54
【问题描述】:

我有一个网站 (www.myexamplewebsite.com),看起来像这样简化:

<html>
<head>
</head>
<body>
  <img src="img/speaker-icon.png" onclick="speakerFunction()">
<script>
  function speakerFunction() {
     var text = new SpeechSynthesisUtterance("This is a test.");
     text.lang = 'en-US';
     window.speechSynthesis.speak(text);                                    
    }
</script>
</body>
</html>

我还有一个简单的 Android 应用程序,它基本上是一个显示我的网站的 web 视图:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView myWebView = (WebView) findViewById(R.id.myWebView);
        myWebView.loadUrl("www.myexamplewebsite.com");
        myWebView.setWebViewClient(new MyWebViewClient());
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
    }

    // Use when the user clicks a link from a web page in the WebView
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals("www.myexamplewebsite.com")) {
                return false;

            }

            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
    }
}

我的问题如下:

当我在我的台式电脑上的普通浏览器(Chrome、Firefox)中访问示例网站时,它可以正常工作。如果我点击网站上的图片,我会听到“这是一个测试”的文字。

如果我在智能手机上使用适用于 Android 的 Chrome-App 访问示例网站,它也可以工作。但是,如果我使用 Firefox-App 或在我自己的应用程序中使用 webview(见上文)访问该网站,则它不起作用。


更新 1: 我明确指定了语言,并使用crosswalk 框架进行了尝试,但这也没有解决问题。

【问题讨论】:

标签: javascript android html text-to-speech


【解决方案1】:

Android webview 不支持SpeechSynthesisUtterance https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesisUtterance

虽然支持 SpeechSynthesis,但由于 SpeechSynthesis 工作基于传入其中的 SpeechSynthesisUtterance 对象,所以它不会仍然工作。

https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesis

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多