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

stari

Active Member
Licensed User
Longtime User
Biswajit, how can i disable beep without muting the sound ?
 

Biswajit

Active Member
Licensed User
Longtime User

Humberto

Active Member
Licensed User
Longtime User
Hi

I develop a software using your library everthing was OK unitl yesterday
Now the beep before and after recognition changes from Music channel to Ring Channel.
The volume channel is '0' using NoRecognizerBeep = true but the we continue listen the beep, just when a put ( manually ) the Volume_Ring to '0" then the beep stop.

Someone has the same problem ?

I use the version library 1.30
 

Biswajit

Active Member
Licensed User
Longtime User
Hi

I develop a software using your library everthing was OK unitl yesterday
Now the beep before and after recognition changes from Music channel to Ring Channel.
The volume channel is '0' using NoRecognizerBeep = true but the we continue listen the beep, just when a put ( manually ) the Volume_Ring to '0" then the beep stop.

Someone has the same problem ?

I use the version library 1.30
The audio channel for the beep sound is dependent upon the Android version. In latest android version it uses music channel but in older version it uses the ring channel. If we try to change the volume of the Ring channel then it will throw an exception if the device is in DnD mode.
 

Humberto

Active Member
Licensed User
Longtime User
It was working until yesterday.
Yesterday start doing this, than I restart the cellphone and it was OK back, but today morning start beeps again.

My android is 9 and the last update was march 01 2020. ( Moto G6 Plus )

There was no system update. The Android just change the channel.
Can I change back ?
 

Biswajit

Active Member
Licensed User
Longtime User
It was working until yesterday.
Yesterday start doing this, than I restart the cellphone and it was OK back, but today morning start beeps again.

My android is 9 and the last update was march 01 2020. ( Moto G6 Plus )

There was no system update. The Android just change the channel.
Can I change back ?
I will test tomorrow. For now you have to mute the sound manually.
 

Humberto

Active Member
Licensed User
Longtime User
IĀ“m testing using MUTE so I can mute any channel and back also.

B4X:
PH.SetMute ( PH.VOLUME_MUSIC, True )
PH.SetMute ( PH.VOLUME_RING, True )
Sr.StartListening(Starter.gLang,False,False)

This way I donĀ“t need to save the volume value of any channel I just set mute off

With music, and whastapp call works OK when I detect Ihen I UNMUTE and the sound is OK.
But When I receive a call, I UNMUTE but still not ringing just vibrate.
It seems that when start ringing changing the mute to off doesnĀ“t affect.
How to do it?

B4X:
Sub PE1_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
'    PhoneStateChanged - The phone State has changed.
'    State - One of the three values: IDLE, OFFHOOK, RINGING. OFFHOOK means that there Is a call Or that the phone Is dialing.
'    IncomingCall - Available when the State value Is RINGING.
    Log ( "State Phone Change: " & State )
    Select State
        Case "IDLE"
'            Timerx.Enabled = True
            gStatusTelefone = "IDLE"
        Case "OFFHOOK"
            Log ( "Desligado Listen") 
            gStatusTelefone = "OFFHOOK"
            Sr.StopListening
            PH.SetMute ( PH.VOLUME_MUSIC, False )
            PH.SetMute ( PH.VOLUME_RING, False )
        Case "RINGING"
            Log ( "Tocando Linha ......")
            gStatusTelefone = "RINGING"
            Sr.StopListening
            PH.SetMute ( PH.VOLUME_MUSIC, False )
            PH.SetMute ( PH.VOLUME_RING, False )
    End Select
End Sub
 
Last edited:

Biswajit

Active Member
Licensed User
Longtime User
IĀ“m testing using MUTE so I can mute any channel and back also.

B4X:
PH.SetMute ( PH.VOLUME_MUSIC, True )
PH.SetMute ( PH.VOLUME_RING, True )
Sr.StartListening(Starter.gLang,False,False)

This way I donĀ“t need to save the volume value of any channel I just set mute off

With music, and whastapp call works OK when I detect Ihen I UNMUTE and the sound is OK.
But When I receive a call, I UNMUTE but still not ringing just vibrate.
It seems that when start ringing changing the mute to off doesnĀ“t affect.
How to do it?

B4X:
Sub PE1_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
'    PhoneStateChanged - The phone State has changed.
'    State - One of the three values: IDLE, OFFHOOK, RINGING. OFFHOOK means that there Is a call Or that the phone Is dialing.
'    IncomingCall - Available when the State value Is RINGING.
    Log ( "State Phone Change: " & State )
    Select State
        Case "IDLE"
'            Timerx.Enabled = True
            gStatusTelefone = "IDLE"
        Case "OFFHOOK"
            Log ( "Desligado Listen")
            gStatusTelefone = "OFFHOOK"
            Sr.StopListening
            PH.SetMute ( PH.VOLUME_MUSIC, False )
            PH.SetMute ( PH.VOLUME_RING, False )
        Case "RINGING"
            Log ( "Tocando Linha ......")
            gStatusTelefone = "RINGING"
            Sr.StopListening
            PH.SetMute ( PH.VOLUME_MUSIC, False )
            PH.SetMute ( PH.VOLUME_RING, False )
    End Select
End Sub
Try the updated (v1.4) library.
 

Humberto

Active Member
Licensed User
Longtime User
Now you mute both channel music and ring and came back with the original volume by yourself.
I donĀ“t need to save the volume and return back right?
I will implement it in my software and I came back with the results.
Thanks
 

Biswajit

Active Member
Licensed User
Longtime User
Now you mute both channel music and ring and came back with the original volume by yourself.
I donĀ“t need to save the volume and return back right?
I will implement it in my software and I came back with the results.
Thanks
There is no volume saving functionality. It will check if your app is allowed to access dnd mode or not. if not then it'll mute the music channel. else it will mute music, system and ring channel. After recognition it will enable those channels.
 

Humberto

Active Member
Licensed User
Longtime User
I need continuously voice recognition so I change your example to start a new recognition after time out or label set.

The first time that the user press the button1 there is no Beep , when , after the time-out or label set the software calls again the Button1_Click the beep is back.

In attach your example with the changes.
 

Attachments

  • example.zip
    10.8 KB · Views: 495

Biswajit

Active Member
Licensed User
Longtime User
I need continuously voice recognition so I change your example to start a new recognition after time out or label set.

The first time that the user press the button1 there is no Beep , when , after the time-out or label set the software calls again the Button1_Click the beep is back.

In attach your example with the changes.
This is not for continuous recognition. Use the forum search to find relevant threads for continuous recognition
 

Humberto

Active Member
Licensed User
Longtime User
Using your example if you press the mic ( "Speak Now') , and when "Recognition Ended" you press again the button in less then one second usually the beep is back.

If you wait more than one second the beep is off.
Just to know.
Thanks
 
Last edited:

Biswajit

Active Member
Licensed User
Longtime User
Using your example if you press the mic ( "Speak Now') , and when "Recognition Ended" you press again the button in less then one second usually the beep is back.

If you wait more than one second the beep is off.

I am aware of that. Sometimes in some devices it takes longer to send the result data back to the app and the beep will trigger only after sending the result. That is why I have added one second pause before resetting the volume.

And that's just an example of how to use it. Disabling the beep is a hack/tricky method. So its natural to have some limitations. You have to add your own conditions in your app for smooth user experience.
 
Last edited:

msmerce

Member
Licensed User
Longtime User
The library works excellently. Thank you very much for writing this great library. Would it be possible to add the "onBufferReceived" event to the library to give the user live feedback?
Thanks in advance.
 

Biswajit

Active Member
Licensed User
Longtime User
The library works excellently. Thank you very much for writing this great library. Would it be possible to add the "onBufferReceived" event to the library to give the user live feedback?
Thanks in advance.
I have tried to add that event but it's not working. You can check the android documentation, it says "There is no guarantee that this method will be called".
 
Top