Android Question CRGoogVR-Library, Google Voice Recog w/ source

canalrun

Well-Known Member
Licensed User
Longtime User
I've uploaded an example B4A app for Google Voice Recognition along with its source code and Eclipse Java source code for the CRGoogVR library I developed.

https://www.dropbox.com/s/ax0nzxf7v1rzsy1/CRGoogVR.zip?dl=0

I've also included my compiled APK so you can just quickly install and try out the example app, if you want.

I placed everything in a zip archive and uploaded to Dropbox - the zip file is likely too large to upload directly to the forum.

To build the CRGoogVR library I used the Simple Library Compiler (SLC) available elsewhere on these forums.

I use Eclipse mainly to take advantage of the IDE editor, Project structure, syntax checking, and tips, but it was not used to build the library. The library was actually built using the SLC.

upload_2017-2-24_19-59-29.png



When I run the SLC, a window similar to the above opens. The string in A points to the Eclipse Project workspace directory for the CRGoogVR library on my computer. The text field B points to where the compiled .jar and .xml files will be placed. I don't know what C does or where the string in this field came from :D.

Unless you want to make changes and compile the library, you can just:
  1. Use the Dropbox link above to download the zip file and extract the B4A code into a B4A project directory.
  2. Copy the CRGoogVR .jar and .xml files to your B4A additional libraries folder.
  3. Compile the B4A app.
  4. (or just install the included APK to your phone).
I'm still using version 4.30 of B4A so you might see a warning when you open the B4A example with a later version. Everything should work, however.

Here's a screenshot of the executing demo B4A app.

upload_2017-2-24_20-7-38.png


  • Click on the Start button and wait for the green "recording" status message. The default Google Speech Input dialog is not displayed.
  • The microphone will capture audio, send it to the Google Speech Recognition API, and the text VR is shown.
  • End of speech will be automatically detected, but you can use the Stop button to end things early.
  • Both interim partial results (pre-pended with PR: ) and the final result (pre-pended with FR: ) are displayed.
  • The Rectangular field toward the upper right will show the microphone RMS value.
This library is similar to my CRBingVR library, but uses the Google Speech API rather than the Microsoft Cognitive Services speech API.

I think the Microsoft API recognizes speech a little better, but the Google API does not require an account or "Key" (or charge a fee) to use their API.

Barry.
 

Attachments

  • upload_2017-2-24_20-7-4.png
    upload_2017-2-24_20-7-4.png
    31.3 KB · Views: 443
Last edited:

Thuong

Member
Licensed User
Longtime User
Dear Canalrun
I just tried your program, but nothing on edText after speaking
although VR example from Erel is working
Help me
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Could it possibly be a language recognition issue?

I know it works with English, and I believe I have the code in the library to select the language to be recognized based on the default language of the phone.
But, this has not been tested and I have no way to test this feature.

Are you trying to recognize a language other than English?

I use this code daily, and it has always worked for me. I've tested it on phones with android 4.x to 6.01.

Please let me know if you are having trouble recognizing a language other than English. There may be a mistake in my library.

Also, please let me know if you do get this working.

Barry.
 
Last edited:
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
It's been almost 6 months since I looked at my library code for this function.
I receive the error 01 also. This usually happens when the microphone does not detect any start of speech.
The way I handle this error in a different app is to show a message and stop speech detection.

It's been a very long time since I've looked at Erel's VR example.

If I remember correctly, he uses the speech Intent. In that case Google would essentially perform the code that my library performs. If they receive this error they may handle it differently – possibly by just ignoring it.

I have never been able to test if my library works correctly with other languages (other than UK English :D). I believe my library is supposed to detect the language of the phone and set the language for VR.

It would be a really big help to me if someone could just briefly test this with another language, maybe French or German. I know Google Speech Recognition does not work for several frequently used languages.

Barry.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
It would be a really big help to me if someone could just briefly test this with another language, maybe French or German.
I just downloaded your lib and run your provided example As is...

I got the following error when i click on START button

LogCat connected to: 9885e6514556383552
--------- beginning of crash
--------- beginning of main
--------- beginning of system
** Activity (main) Create, isFirst = true **
CRGoogVR B4A Library ver:1.21
** Activity (main) Resume **
CRGoogVR Initialize - EventName:CRGVR, Package:canalrun.apps.crgvr
** Activity (main) Pause, UserClosed = false **
main_activity_pause (java line: 395)
java.lang.IllegalArgumentException: Service not registered: android.speech.SpeechRecognizer$Connection@d2cbb3f
at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1352)
at android.app.ContextImpl.unbindService(ContextImpl.java:1536)
at android.content.ContextWrapper.unbindService(ContextWrapper.java:700)
at android.speech.SpeechRecognizer.destroy(SpeechRecognizer.java:408)
at canalrun.com.crgvr.CRGoogVR.DestroyVR(CRGoogVR.java:171)
at canalrun.apps.crgvr.main._activity_pause(main.java:395)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at canalrun.apps.crgvr.main.onPause(main.java:266)
at android.app.Activity.performPause(Activity.java:7103)
at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1330)
at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:4126)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:4103)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:4066)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:4024)
at android.app.ActivityThread.-wrap18(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1647)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
java.lang.IllegalArgumentException: Service not registered: android.speech.SpeechRecognizer$Connection@d2cbb3f
** Activity (main) Resume **
CRGoogVR Initialize - EventName:CRGVR, Package:canalrun.apps.crgvr
main_bnstart_click (java line: 427)
java.lang.IllegalArgumentException: Service not registered: android.speech.SpeechRecognizer$Connection@ee9cf6a
at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1352)
at android.app.ContextImpl.unbindService(ContextImpl.java:1536)
at android.content.ContextWrapper.unbindService(ContextWrapper.java:700)
at android.speech.SpeechRecognizer.destroy(SpeechRecognizer.java:408)
at canalrun.com.crgvr.CRGoogVR.StartVR(CRGoogVR.java:147)
at canalrun.apps.crgvr.main._bnstart_click(main.java:427)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:6213)
at android.widget.TextView.performClick(TextView.java:11074)
at android.view.View$PerformClick.run(View.java:23645)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
java.lang.IllegalArgumentException: Service not registered: android.speech.SpeechRecognizer$Connection@ee9cf6a
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Thanks for giving it a try.

I'm definitely no library expert, but I have a feeling the root of the error lies in: java.lang.IllegalArgumentException: Service not registered: android.speech.SpeechRecognizer$Connection@d2cbb3f

When the application starts it tries to initialize the Google Speech Recognizer service.

I can't remember whether I check for the recognizer being initialized before I try to initiate speech recognition when the Start button pressed, but in this example app probably not.

When it starts up, watch for a toast message saying Speech Recognizer Not Available. Again I don't recall if that is in this example app.

I'm not positive, but I bet this could be the error.

Which brand phone and version of Android are you using?

What is the default language for the phone?

Speech recognition works in other apps?

Thanks,
Barry.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
but I have a feeling the root of the error lies in
yes. Sounds like there is a missing SERVICE declaration in the manifest. Maybe.
When it starts up, watch for a toast message saying Speech Recognizer Not Available
i dont see any toast at startup
Which brand phone and version of Android are you using?
Samsung S7 EDGE. Android 7.01
What is the default language for the phone?
German
Speech recognition works in other apps?
Dont know. Never tried... I downloaded the lib here just to help you testing it on other languages. I wanted to try german language (my native language)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I just tried another device. A 9" tablet (Samsung) running android 5.
Here it is working
vr.png

Note that the recognition of german language is perfect in this case

I just tried a longer text and it regocnized all correctly. WOW

PD: I´ll check the library source and see if i can find a solution for my S7 (Android 7)....
 
Last edited:
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Thanks.

Could very well be Android 7.01. The highest version Android device I have is 6.01.

I'm glad to see the other working in German. I'll have to find my high school German dictionary to try to translate :).

If you know what I would need to do support 7.01, that would be helpful. I'll have to try to dig up a 7.01 phone.

Barry.

Added later:

???
It was once an old castle and Kunibert so the boss was called
???
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
i ust found this answer on stackoverflow. https://stackoverflow.com/questions/13227105/speechrecognizer-not-connected-to-recognition-service

Not destroying the SpeechRecognition object in onPause method would block other apps from using SpeechRecognition service


@Overrideprotectedvoid onPause(){

Log.i(CLASS_TAG,"on pause called");if(sr!=null){
sr.stopListening();
sr.cancel();
sr.destroy();

}
sr =null;

super.onPause();}
Your code is just destroying the object... you do not call stop listening and cancel before.
maybe this can help
 
Upvote 0

Thuong

Member
Licensed User
Longtime User
Dear Canalrun
I found your demo app is running OK on android 4.4.2 (my android TV box and old phone) by Vietnamese language
but error 01 on android 6.0.1 (Xiaomi MIUI 7), nothing display, and log : err 01

Wait your libary
Thank
 
Last edited:
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
It's amazing how quickly one forgets about the details of a project. I worked on this about six months ago and I have forgotten all this stuff :(.

When I was doing this, I specifically set the target SDK low hopefully avoiding the need for using Runtimepermissions.

B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetApplicationAttribute(android:theme, "@android:style/Theme.Holo.Light")
'End of default text.
AddPermission(android.permission.RECORD_AUDIO)
AddPermission(android.permission.INTERNET)

Is it the Record_Audio permission that's dangerous?

I tested with 6.01, but maybe 7.01 behaves differently?

Now that 7+ is becoming more widely used I will have to revisit this.

Barry.


Added Later: (Some documentation I found)

As of API level 23, the following permissions are classified as PROTECTION_NORMAL:

ACCESS_LOCATION_EXTRA_COMMANDS
ACCESS_NETWORK_STATE
ACCESS_NOTIFICATION_POLICY
ACCESS_WIFI_STATE
BLUETOOTH
BLUETOOTH_ADMIN
BROADCAST_STICKY
CHANGE_NETWORK_STATE
CHANGE_WIFI_MULTICAST_STATE
CHANGE_WIFI_STATE
DISABLE_KEYGUARD
EXPAND_STATUS_BAR
GET_PACKAGE_SIZE
INSTALL_SHORTCUT
INTERNET
KILL_BACKGROUND_PROCESSES
MODIFY_AUDIO_SETTINGS
NFC
READ_SYNC_SETTINGS
READ_SYNC_STATS
RECEIVE_BOOT_COMPLETED
REORDER_TASKS
REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
REQUEST_INSTALL_PACKAGES
SET_ALARM
SET_TIME_ZONE
SET_WALLPAPER
SET_WALLPAPER_HINTS
TRANSMIT_IR
UNINSTALL_SHORTCUT
USE_FINGERPRINT
VIBRATE
WAKE_LOCK
WRITE_SYNC_SETTINGS
and

Dangerous permissions :

READ_CALENDAR
WRITE_CALENDAR
CAMERA
READ_CONTACTS
WRITE_CONTACTS
GET_ACCOUNTS
ACCESS_FINE_LOCATION
ACCESS_COARSE_LOCATION
RECORD_AUDIO
READ_PHONE_STATE
CALL_PHONE
READ_CALL_LOG
WRITE_CALL_LOG
ADD_VOICEMAIL
USE_SIP
PROCESS_OUTGOING_CALLS
BODY_SENSORS
SEND_SMS
RECEIVE_SMS
READ_SMS
RECEIVE_WAP_PUSH
RECEIVE_MMS
READ_EXTERNAL_STORAGE
WRITE_EXTERNAL_STORAGE


Android Developer's Page:
https://developer.android.com/guide/topics/permissions/requesting.html#normal-dangerous

Erel's Requesting Runtime Permissions:
https://www.b4x.com/android/forum/threads/runtime-permissions-android-6-0-permissions.67689/#content
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Hi Barry,
Is this implementation better than your wonderful CRBingVR?
If so, can you briefly describe why?
Can you send a .Wav file or other?
Regards,
Rusty
 
Last edited:
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
Happy Holidays.
Personally I use CRGoogVR rather than my alternative CRBingVR.

The two are slightly different.

CRBingVR uses the Microsoft Cognitive Services Voice recognition engine. This operates by having my library capture microphone input and then sending the audio buffers to Microsoft for voice recognition when speech is detected. This sounds nice and sounds like it gives you more control, but it means the library is responsible for detecting the start and duration of speech – even within a lot of background noise.

I signed up for a Microsoft Cognitive Services demo key a couple years ago. Since then it no longer reports that my key exists, although the key still works.

You now have to sign up through Microsoft Cognitive Services rather than Bing and I have never figured out how to do that. It seems it always leads me around in a "rat race" never taking me to the spot to actually sign up.

On the plus side: Microsoft Cognitive Services speech recognition did seem to perform a little better than my CRGoogVR implementation in my testing.

The CRGoogVR library uses the free Android Speech Recognition API. I really should've named the library CRAndroidVR.

The Android Speech Recognition API takes care of capturing microphone input. I think in the course of doing that they detect the start and duration of speech and they also handle noisy situations pretty well. Properly handling Noisy environments can be a real challenge.

The CRGoogVR library bypasses the standard pop-up that appears in most implementations using Android's Speech API.

My ultimate goal is to achieve Continuous Speech Recognition. The Android API limits the duration of detected speech to approximately 45-60 seconds or so.

The Microsoft Cognitive Services implementation theoretically should allow continuous speech recognition. The library will control when detected speech is sent to Microsoft and the library should be able to detect pauses in speech allowing it to stop sending to Microsoft. The Microsoft speech recognition engine puts a limit, I think 60 seconds, on the length of a series of audio buffers representing a single utterance it will support.

Another thing is that the Android API is free while the Microsoft Cognitive Services library will charge per 15 second snippet after some amount of time.

There is also another service, Google Cloud Services, which is supposed to handle continuous recognition. They also charge. Speech recognition is currently limited to about 60 seconds, I think. I don't have a library for Google Cloud Services.

I hope this helps.

I mentioned continuous speech recognition and command recognition in a Chat thread about starting a Community Project. I think it would be a lot of fun to attack this problem as a group.

Barry.
 
Last edited:
Upvote 0
Top