1

我想知道响应式语音(JS)什么时候说完。在此处的文档中,它显示我可以传递 onstart/onend 之类的回调。因此,我制作了一个 JSInterface 并将其附加到调用我的 JS 函数的 WebView 上。但是我不能正确地做,因为我的回调从未输入过。

这是我的 JS 接口:

private class JSInterface{
    @JavascriptInterface
    public void alertStart(){
        Log.i(TAG, "Speech started");
    }

    @JavascriptInterface
    public void stopService(){
        stopSelf(); //The whole point of this is to stop my Service.
    }
}

这里我将接口附加到webview:

final WebView webView = new WebView(context);
webView.setSoundEffectsEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new JSInterface(), "SpeechService");

这是我的 JS 函数:

function speak(text,voice,pitch,rate) {
    responsiveVoice.speak(text, voice,
        {
            onstart: alertStart,
            onend: stopService
        }
    );
}
function alertStart(){
    SpeechService.alertStart();
}

function stopService(){
    SpeechService.stopService();
}

我在这里的服务中使用我的 webView 调用该函数:

webView.setWebViewClient(new WebViewClient() {
        public void onPageFinished(WebView view, String url) {
            if (sp.contains("voiceSettings")) {
                String voiceSettings = sp.getString("voiceSettings", "Voice settings not available");
                final String splitSettings[] = voiceSettings.split(":");
                Log.i("settings", Arrays.toString(splitSettings));
                webView.loadUrl("javascript:speak('" + text + "','" + splitSettings[0] + "','" + Double.parseDouble(splitSettings[1]) + "','" + Double.parseDouble(splitSettings[2]) + "')");
            }
            else{
                webView.loadUrl("javascript:speak('" + text + "','" + "UK English Male" + "','" + 1 + "','" + 1 + "')");
            }
        }
    });
    webView.loadUrl("file:///android_asset/tts.html");
}

提前致谢

4

1 回答 1

0

原来,被发送到 JS 函数的字符串中有一个撇号,这导致语法不正确。所以该函数从未被调用。必须用双引号将输入包装起来,以便包装所有撇号。

于 2016-07-11T14:26:45.970 回答