3

I am facing a rather annoying problem: For a customer project I need to have some sort of "end of speech"-detection utilizing HTML5/JavaScript (customer specification). So I did some research and thought "well, let´s try the SpeechRecognition API" and it´s "onspeechend"-Event. This works like charm on my machine but not in the Application itself. During my research I found, that the WebView you use within your Apps is actually not the Systems default browser.

The userAgent of the default browser returns me

Mozilla/5.0 (Linux; Android 6.0.1; SM-G900F Build/MMB29M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/54.0.2840.68 Mobile Safari/537.36

however, the WebView-userAgent return

Mozilla/5.0 (Linux; Android 6.0.1; SAMSUNG SM-G900F Build/MMB29M) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/4.0 Chrome/44.0.2403.133 Mobile Safari/537.36

The disturbing part is, that the SpeechRecognition-API is available in the default browser, but not in the WebView-instance. Anyone an idea how to fix/work around this?

Or any ideas, how to detect the end of speech. Unfortunately, using the SpeechRecognition-Intent is NOT an option. I would like to do it that way, but as mentioned, the customer insists on doing this with HTML5/JavaScript.

codingbuddha
  • 707
  • 5
  • 16
  • Why exactly is there the requirement to not use native methods? – Force Nov 02 '16 at 15:19
  • Because the customer wants to "reuse the code for other web-based applications". Don´t ask, I am not amused by this either since this causes a lot more work than needed. – codingbuddha Nov 03 '16 at 09:57
  • Might it be an idea to not use native at all and go with a Cordova App? *note, while 1 codebase at times is great, it tends to lead to compromises of the lowest denominator, and miss out on some great stuff.* – Flummox - don't be evil SE Nov 07 '16 at 10:56
  • Does not solve the problem, since the requirement is not met. If we write a Cordova-App, we still dont have the main part written in re-useable JavaScript. – codingbuddha Nov 07 '16 at 11:13
  • So your app is pure html/js? No cordova wrapper or something like that? Then I don't see way how this can be solved except prompting user to install chrome for android. Just curious why you mentioned SpeechRecognition-Intent though. You can't use those kind of intents from browser, or can you? – mauron85 Nov 08 '16 at 19:11
  • you could write polyfil specific for SAMSUNG browser to record voice over webRTC (or any available voice API), and send results to SpeechRecognition service. However I doubt it has open API. So maybe some alternative, but not sure about quality of such a thing. – mauron85 Nov 08 '16 at 19:25
  • Just checked out and there is Google Speech API in beta available. So technically you can record voice via webRTC and send it to your nodejs server for recognition. The sample implementation of such a server https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/speech. However it would require some effort and definitely it would not be an easy pie. Also webRTC will record uncompressed audio so you'll have to compress in on the client somehow or your customers will have to accept larger outbound data traffic. – mauron85 Nov 08 '16 at 19:29
  • 1. The app is certainly NOT pure HTML/JS, how could it. It is a Java-wrapper which displays HTML/JS. 2. The process of recording audio and sending it to the customers ASR-System works as charm. So no problems here. – codingbuddha Nov 09 '16 at 11:42
  • Have you requested permissions to capture Audio/Video in Android app? – Andrii Muzalevskyi Sep 27 '17 at 20:19

2 Answers2

1

I have implemented the default speech recognition in Android.

after lots of resource digging, I found that you just need to give permission through webviewchromeclient

Note: This is a working solution.

    webView.setWebChromeClient(new WebChromeClient()
        {

            @Override
            public void onPermissionRequest(final PermissionRequest request) {
                request.grant(request.getResources());
            }
}

Cheers!

Raut Darpan
  • 1,520
  • 9
  • 14
  • I have tried this but it didn't work. How did you get yours to work? I mean, does speech to text really work in android webview? – DTambah Jan 27 '23 at 22:18
0

Seems the problem is that webview is using default device rendering engine (as explained in this answer). And that default engine is based on Chrome 44, which probably doesn't support API you want to use, which is available in Chrome 54 of default browser.

Try another implementation of rendering engine, for example Crosswalk. With it you can ensure that particular chrome version is used by switching to appropriate crosswalk release version as a webview in your app. Although embedding it would increase your apk size, and using it shared mode requires that user installs crosswalk runtime from google play.

Community
  • 1
  • 1
shtolik
  • 1,341
  • 22
  • 29