レスポンシブボイス (JS) がいつ話し終わったか知りたいです。ここのドキュメントでは、onstart/onend などのコールバックを渡すことができることを示しています。そこで、JSInterface を作成し、JS 関数を呼び出す WebView にアタッチしました。ただし、コールバックが入力されないため、正しく実行してはいけません。
これが私のJSInterfaceです:
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");
}
前もって感謝します