1

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

sadelbrid
  • 411
  • 1
  • 4
  • 16
  • Where you are calling speak function? Have you declared the voice string? – Prasad Jul 10 '16 at 08:37
  • I miscopied the js speech header from my code, but I updated it. I also added the code that calls the function. You'll see that I'm trying to pass it a voice, pitch, and volume as well. Also, right now it only enters that else condition – sadelbrid Jul 10 '16 at 15:23

1 Answers1

0

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.

sadelbrid
  • 411
  • 1
  • 4
  • 16