B4A Library SpeechRecognitionNoUI - Google Speech Recognition Without Popup

A better alternative for offline recognition is available here.

With this, you can add speech recognition feature to your application without google speech recognition popup (check attached example),

SpeechRecognitionNoUI

Author:
@Biswajit
Version: 1.6
  • SpeechRecognitionNoUI
    • Events:
      • ReadyForSpeech
        Called when the endpointer is ready for the user to start speaking.
      • BeginningOfSpeech
        The user has started to speak.
      • EndOfSpeech
        The user stops speaking.
      • Error (Msg As String)
        A network or recognition error occurred.
      • PartialResults (Texts As List)
        Partial recognition results are available.
      • Results (Texts As List)
        Recognition results are ready.
      • RmsChanged (RmsValue As Int)
        The sound level in the audio stream has changed. There is no guarantee that this method will be called.
      • BufferReceived(buffer() As Byte)
        The purpose of this function is to allow giving feedback to the user regarding the captured audio. There is no guarantee that this method will be called.
    • Functions:
      • NoRecognizerBeep As Boolean
        Set true if you dont want beep sound before and after speech recognition.
        Starting from Android 7+ this method will throw an exception if the user set the Do Not Disturb mode,
        unless your app has requested a special permission with NOTIFICATION_POLICY_ACCESS_SETTINGS.
        (Check example)
      • Initialize (eventname As String, callback As Object) As Boolean
      • IsInitialized As Boolean
      • IsRecognitionAvailable As Boolean
        Check if Speech Recognition is available or not
      • StartListening (LANGUAGE As String, PARTIAL_RESULTS As Boolean, PREFER_OFFLINE As Boolean) As String
        Start listening.
        LANGUAGE : Recognition language. IETF language tag (as defined by BCP 47), for example "en-US"
        PARTIAL_RESULTS : indicate whether partial results should be returned by the recognizer as the user speaks.
        PREFER_OFFLINE : indicate whether to only use an offline speech recognition engine. If false then either network or offline recognition engines may be used
      • StopListening As String

Here is the original thread. I took some ideas and codes to build this library.

Note: Remember to INTERNET, RECORD_AUDIO and NOTIFICATION_POLICY_ACCESS_SETTINGS permission.
For SDK v30: Add this following code to your project manifest.
B4X:
AddManifestText(
<queries>
    <intent>
        <action android:name="android.speech.RecognitionService" />
    </intent>
</queries>)

Update 1.1: Added NoRecognizerBeep option. Set true if you don't want beep sound before and after speech recognition.
Update 1.2:
  1. Added ReadyForSpeech event.
  2. Fixed recognition language problem
Update 1.3: Fixed crashing issue in DND mode.
Update 1.4: Added proper error handling and warning message for DND mode. (Check the example)
Update 1.5: Added BufferReceived event.
Update 1.6: Fixed an issue with the offline voice recognition.
 

Attachments

  • SpeechRecognitionNoUI_example.zip
    10.5 KB · Views: 1,660
  • SpeechRecognitionNoUI_v1.6.zip
    7.5 KB · Views: 987
Last edited:

msmerce

Member
Licensed User
Longtime User
Thank you for your quick response. I've already read the Android documentation for "RecognitionListener". By the way, "onRmsChanged" also reads "There is no guarantee ...". Nevertheless the event is triggered. Probably this sentence only means that the event is not triggered on all devices. Is the event just not fired on your device, or is there an error when adding the event? Would it be possible to add the event so that everyone can try it for themselves?
 

Biswajit

Active Member
Licensed User
Longtime User
Thank you for your quick response. I've already read the Android documentation for "RecognitionListener". By the way, "onRmsChanged" also reads "There is no guarantee ...". Nevertheless the event is triggered. Probably this sentence only means that the event is not triggered on all devices. Is the event just not fired on your device, or is there an error when adding the event? Would it be possible to add the event so that everyone can try it for themselves?
Update 1.5: Added BufferReceived event.
 

sn_nn

Member
Licensed User
Longtime User
Hi, Biswajit! Very nice library, but it working online only. Then i try work offline, i'm receiving message: "Network error". (Android 7.0). Is it possible work offline?
 

Biswajit

Active Member
Licensed User
Longtime User
Hi, Biswajit! Very nice library, but it working online only. Then i try work offline, i'm receiving message: "Network error". (Android 7.0). Is it possible work offline?
Don't know. I haven't faced this before. But as per stackoverflow if you are using a samsung device then change the speech API in the phone settings.
Also check if your device has the language (which you are using in your code) pack installed or not.
 

sn_nn

Member
Licensed User
Longtime User
Don't know. I haven't faced this before. But as per stackoverflow if you are using a samsung device then change the speech API in the phone settings.
Also check if your device has the language (which you are using in your code) pack installed or not.
Oukitel 10000K pro (Android 7.0), offline voice recognising is working with XSpeechRecogniser...
Configuration ia enabling offline recognising too.
 

Biswajit

Active Member
Licensed User
Longtime User
Oukitel 10000K pro (Android 7.0), offline voice recognising is working with XSpeechRecogniser...
Configuration ia enabling offline recognising too.
Could you post your code, how you are calling the StartListening method?
 

pas

Member
Licensed User
Longtime User
When using the sample code, it does not work when the the device is offline (WiFi disabled). Could somebody please describe any steps needed for this to work? Note the device being tested is running Android 9 and has the offline speech recognition files loaded and enabled via settings. Thank you.
 

kalikamtest

New Member
Hi friends, I did everything right, but what is the reason for this error?
test on xiaomi redmi 6 android 9 & Huawei y6

==================
Logger connected to: Xiaomi Redmi 6
--------- beginning of main
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/core/content/ContextCompat;
at anywheresoftware.b4a.objects.RuntimePermissions.Check(RuntimePermissions.java:73)
at anywheresoftware.b4a.objects.RuntimePermissions.CheckAndRequest(RuntimePermissions.java:97)
at ir.yahoo98.brandsum.main._activity_resume(main.java:461)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at ir.yahoo98.brandsum.main.afterFirstLayout(main.java:111)
at ir.yahoo98.brandsum.main.access$000(main.java:17)
at ir.yahoo98.brandsum.main$WaitForLayout.run(main.java:83)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6820)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:922)
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.core.content.ContextCompat" on path: DexPathList[[zip file "/data/app/ir.yahoo98.brandsum-JvTw7EfiUeeFKtvkPcgchA==/base.apk"],nativeLibraryDirectories=[/data/app/ir.yahoo98.brandsum-JvTw7EfiUeeFKtvkPcgchA==/lib/arm, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:171)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 20 more
 

Biswajit

Active Member
Licensed User
Longtime User
Hi, Google Speech limition Time is 60 seconds, but this library limition is 2. is any solution for it?
There is no limitation added externally to the library its a bridge between b4a to native android. Its based on the android SpeechRecognizer.
There is no option to extend the time limitation. If it detects silence for 2-3 sec then it will stop.

Google Speech limition Time is 60 seconds
Where did get this information?
 

PodSoft2016

Member
Sorry sir. i checked again and saw in silent mode limit time for disconnection is 6 seconds. its sufficient time for speaking,but 2 is less. as this code
 

Attachments

  • ax.png
    ax.png
    65.9 KB · Views: 277

PodSoft2016

Member
im so sorry sir. i checked language fa is 2 seconds and other language is 6 seconds. its strange but real. Tnx For your good library
 

Biswajit

Active Member
Licensed User
Longtime User
There is nothing I can do with the timing. Everything is happening in the system. The library is just invoking the process without the popup.
 

Biswajit

Active Member
Licensed User
Longtime User
Top