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: 688
  • SpeechRecognitionNoUI_v1.6.zip
    7.5 KB · Views: 52
Last edited:

Guenter Becker

Active Member
Licensed User
  1. Open Google App
  2. click on the "more" button at the bottom right corner
  3. goto settings
  4. goto voice
  5. click on offline speech recognition
  6. goto All tab
  7. click on the language pack you want to use for offline speech recognition.

This is very helpfull, thank you.
Going the way on my phone I identify that two languages (German and English US) are already installed!
Next I took your B4A Example and changed the parameters: sr.StartListening("tested this lng codes: en / de / DE / de-US / DE-US / GE / GE-US",TRUE,TRUE) nothing works, always 'No Match'.
Changing Back to: sr.StartListening("tested this lng codes: en / de ",TRUE,FALSE) it works.

I looked on the Google Developer API Page ( Languages Codes | Directory API | Google Developers ). There is a list of lng codes and for Germany it says it's 'de'.

At the end if I set offline=false it works, setting it to true it works not!
 

Guenter Becker

Active Member
Licensed User
Hello Biswajit,
Do you have time to give me an answer/idea why the lib will not work in Offline Mode?
 

bsnqt

Active Member
Licensed User
Hi Guenter Becker and All

Same to me, it does not work if I set the PREFER_OFFLINE parameter to "False"... Try many ways but it does not work at all. Actually I also cannot change the language as well (have checked my Google setting for downloaded language package).
 

Biswajit

Active Member
Licensed User
Hello Biswajit,
Do you have time to give me an answer/idea why the lib will not work in Offline Mode?
Hi Guenter Becker and All

Same to me, it does not work if I set the PREFER_OFFLINE parameter to "False"... Try many ways but it does not work at all. Actually I also cannot change the language as well (have checked my Google setting for downloaded language package).

After so many testing finally I found the issue. Now android takes all the parameters for SpeechRecognizer as string that's why it wasn't working. I will post the update in a moment.
 

Guenter Becker

Active Member
Licensed User
Hi bsnqt
sr.StartListening("de",True,False)

Notice! 'de' stands for German. Abbreviation/LNG Code must be taken from Google Dev!
 

Guenter Becker

Active Member
Licensed User
Biswajit
great and well done awaiting your update. Stay well.
 

bsnqt

Active Member
Licensed User
Hi bsnqt
sr.StartListening("de",True,False)

Notice! 'de' stands for German. Abbreviation/LNG Code must be taken from Google Dev!
Thank you, appreciate.
Yes I can change the language now. The only issue left now is the offline preference parameter, I hope we can get it done soon from Biswajit's update 😀
 

bsnqt

Active Member
Licensed User
Just a quick report to Biswajit... I downloaded the lib 1.6 few minutes ago (as I got an update notification). Have checked and can see that the offline feature does not work yet. Please ignore this message if you have not yet updated it.

Screenshot_20210926-212038.jpg
 

Biswajit

Active Member
Licensed User
Just a quick report to Biswajit... I downloaded the lib 1.6 few minutes ago (as I got an update notification). Have checked and can see that the offline feature does not work yet. Please ignore this message if you have not yet updated it.

View attachment 119604
New version of google now app will automatically try to use online speech recognition when the offline language is not installed on the device. Try to install that language and then try again. And while using offline speech recognition you have to specify the country code along with the language code. "en" will not work where "en-US" will work for the offline mode.
 

bsnqt

Active Member
Licensed User
Hi Biswajit,

Sorry I am still unsuccessful. Now I can make it done with "en-US" offline, but could not yet make it with my local language. I am using Samsung Galaxy phone. Where I can download the offline Speech-To-Text language package. It is a little confusing for me.

First, I can see that Google Speech Services say "now all voices for the language downloaded together"?

allvoices.jpg


I also have selected Google Services as the default as per what they require (pls see pictures attached)
But still can select the local language but on the other setting I can see only English?
I missed something or doing totally wrong thing? Where I can download the offline language package?
 

Attachments

  • only english.jpg
    only english.jpg
    146 KB · Views: 10
  • selected2.jpg
    selected2.jpg
    497.1 KB · Views: 11

Guenter Becker

Active Member
Licensed User
Hello, together,

with the new update the correct language code (sr.startlistening("de-DE",True,True) and the language downloaded from Google Dev the lib is now working as followes:
  • The correct language is taken. In my way de-DE for German (ISO LNG Codes)
  • Online Mode works correct.
  • Offline Mode no more errors reported but.....
  • Offline mode for example speaking 123 the lib returns 12312 12 3 ???? Tried it with different values always the same.

There must be still something wrong.

By the way I downloaded the offline language file its one per language and not the language package.
 

Biswajit

Active Member
Licensed User
Hi Biswajit,

Sorry I am still unsuccessful. Now I can make it done with "en-US" offline, but could not yet make it with my local language. I am using Samsung Galaxy phone. Where I can download the offline Speech-To-Text language package. It is a little confusing for me.

First, I can see that Google Speech Services say "now all voices for the language downloaded together"?

View attachment 119630

I also have selected Google Services as the default as per what they require (pls see pictures attached)
But still can select the local language but on the other setting I can see only English?
I missed something or doing totally wrong thing? Where I can download the offline language package?
Open google app, click the three dot icon at the bottom right corner. Then settings > voice > offline speech recognition then download the language pack you want.
 

Biswajit

Active Member
Licensed User
Hello, together,

with the new update the correct language code (sr.startlistening("de-DE",True,True) and the language downloaded from Google Dev the lib is now working as followes:
  • The correct language is taken. In my way de-DE for German (ISO LNG Codes)
  • Online Mode works correct.
  • Offline Mode no more errors reported but.....
  • Offline mode for example speaking 123 the lib returns 12312 12 3 ???? Tried it with different values always the same.

There must be still something wrong.

By the way I downloaded the offline language file its one per language and not the language package.
If its returning incorrect results then its related to the speech recognition engine. The library just create a bridge between B4A code and the android speech recognition API.
 

bsnqt

Active Member
Licensed User
Hi Biswajit,

Thanks for your reply and help to me and forum.
I confirm the library now works offline for "en-US". But for my local language (Vietnamese) it does not work at all and give a message saying "Network error".

I am not sure which Google app (by Google LLC) you are talking about, I am afraid that I may misunderstand you so I attached here the screenshot of "my" Google app, where I don't see any three dot icon even I have go thru to the bottom of the app screen. Did I go to wrong app?

screenshot.jpg


(Instead I did go to Settings -> Google Voice Typing -> Voice Typing Language. And I already downloaded the local language, however voice recognition also does not work offline)
 

Biswajit

Active Member
Licensed User
Hi Biswajit,

Thanks for your reply and help to me and forum.
I confirm the library now works offline for "en-US". But for my local language (Vietnamese) it does not work at all and give a message saying "Network error".

I am not sure which Google app (by Google LLC) you are talking about, I am afraid that I may misunderstand you so I attached here the screenshot of "my" Google app, where I don't see any three dot icon even I have go thru to the bottom of the app screen. Did I go to wrong app?

View attachment 119652


(Instead I did go to Settings -> Google Voice Typing -> Voice Typing Language. And I already downloaded the local language, however voice recognition also does not work offline)
Yes this is the right app. Google voice typing will not work. The android speech recognition API actually uses a hidden service of the google search app. So you have to download the language from that app.

I think you are using a tab. So the option may be placed somewhere else for that screen size. Check the screenshot below.
WhatsApp Image 2021-09-27 at 9.11.43 PM.jpeg
 

bsnqt

Active Member
Licensed User
Oh, now I understand... that is why I could not find it out (the three dots). Actually I tried (thought maybe it is hidden) but without success, there is no more tab in the app, just only 4 as shown in the picture I attached earlier. For an unknown reason, "my" Google app does not have it 😰 . Very strange.

I have only Samsung phones at home (the screenshot was taken from Galaxy Z Fold2). I will need to borrow somebody's phone with other brand to look at. Or Samsung may customize the apps (which I think impossible for the case of Google)? I will update you. Thank you very much.

(Next question I ask myself is then how can I download the package?)
 

Guenter Becker

Active Member
Licensed User
Hello hope you are well,

up to the problems with the OFFline Mode I found a workaround. In my App I need only numbers as spoken input. Using the code below it will work with the correct return either in Online or in Offline Mode. As biswajit says the problem does not come from his lib! It is comming from the speech recognition engine. In Offline mode it returns more than one value and the PartialResults sub is used, don't know why:

B4X:
Sub sr_Results(Texts As List)
    Dim Text As String
    For Each t As String In Texts
        Text = Text & t
    Next
    If IsNumber(Text) Then    DLGinputEdt.Text = Text
End Sub

Sub sr_PartialResults(Texts As List)
    If Texts.Size > 0  Then
        Dim Text As String = Texts.Get(0)
        If IsNumber(Text) Then    DLGinputEdt.Text = Text
    End If
End Sub
 
Last edited:

Biswajit

Active Member
Licensed User
A better alternative for offline recognition is available here.
 
Dear Mr Biswajit,
Please accept my deepest possible appreciation to you. You are allowing B4A programmers to use The best available and up to date voice recognition technology. Very very well done. Yours Java knowledge is outstanding, because you are even familiar with JNI standard to access native .so libraryes. Sure, Google recognizer do not require this programmers knowledge. But yours other big programmers library in The thread continuous voice recognition require that. So thank you for yours excellent activity on this forum.
 
Top