Android WebView (WebChromeClient) 语音合成未定义

Str*_*rce 8 html javascript text-to-speech webview chromium

我正在尝试在已设置为 WebChromeClient 的 Android WebView 中使用 Web 语音合成 API。但是,当我检查页面并运行以下命令时, console.log('speechSynthesis' in window, 'webkitSpeechRecognition' in window, 'speechRecognition' in window);每个命令都会返回 false、false、false。

如果我在 Android 上使用 Chrome 检查页面,那么它会返回 true、true、false。

通过 Chrome 应用程序打开的用户代理显示为:

“Mozilla/5.0(Linux;Android 8.1.0;Pixel 2 XL Build/OPM1.171019.018)AppleWebKit/537.36(KHTML,如 Gecko)Chrome/64.0.3282.137 Mobile Safari/537.36”

Chrome WebView 显示为:

“Mozilla/5.0(Linux;Android 8.1.0;Pixel 2 XL Build/OPM1.171019.018;wv)AppleWebKit/537.36(KHTML,如 Gecko)版本/4.0 Chrome/64.0.3282.137 Mobile Safari/537.36”

Chrome版本完全相同,但speechSynthesis不在窗口中。

我在网络视图上启用了以下设置:

    webViewSettings.setJavaScriptEnabled(true);
    webViewSettings.setAllowFileAccessFromFileURLs(true);
    webViewSettings.setAllowUniversalAccessFromFileURLs(true);
    webViewSettings.setAllowContentAccess(true);
    webViewSettings.setAllowFileAccess(true);
    webViewSettings.setDatabaseEnabled(true);
    webViewSettings.setMediaPlaybackRequiresUserGesture(false);
Run Code Online (Sandbox Code Playgroud)

谢谢

小智 0

代码是正确的,但您需要将代码从 更改webViewSettingswebview.getSettings()