I want to know when responsivevoice (JS) is done speaking. In the docs here, it shows I can pass callbacks like onstart/onend. So, I made a JSInterface and attached it to the WebView that calls my JS function. However I must not be doing it correctly because my callbacks are never entered.
Here is my 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.
}
}
Here I attach the interface to the webview:
final WebView webView = new WebView(context);
webView.setSoundEffectsEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new JSInterface(), "SpeechService");
And here are my JS functions:
function speak(text,voice,pitch,rate) {
responsiveVoice.speak(text, voice,
{
onstart: alertStart,
onend: stopService
}
);
}
function alertStart(){
SpeechService.alertStart();
}
function stopService(){
SpeechService.stopService();
}
And I call the the function with my webView from my service here:
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");
}
Thanks in advance
Turns out, the string that was being sent to the JS function had an apostrophe in it, which caused improper syntax. So the function was never called. Has to wrap the input in double quotes so that all apostrophes are wrapped.