B4A Library [Moved] SpeechRecognitionNoUI

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Tried this in my music app. Wanted to be able to shout Music Skip, Music Play, Music Pause, etc

Problem is it is always beeping and if I turn off the beeps the music gets paused.

Tried to change the code to not mute the music volume but then it beeps.

B4X:
Public Sub StartListening(LANGUAGE As String, ONLY_RETURN_LANGUAGE_PREFERENCE As Boolean, PARTIAL_RESULTS As Boolean,PREFER_OFFLINE As Boolean)
   RecognizerIntent.Initialize("android.speech.action.RECOGNIZE_SPEECH",Null)
   RecognizerIntent.PutExtra("android.speech.extra.LANGUAGE_PREFERENCE",LANGUAGE)
   If ONLY_RETURN_LANGUAGE_PREFERENCE Then RecognizerIntent.PutExtra("android.speech.extra.ONLY_RETURN_LANGUAGE_PREFERENCE",LANGUAGE)
   RecognizerIntent.PutExtra("calling_package",Application.PackageName)
   RecognizerIntent.PutExtra("android.speech.extra.LANGUAGE_MODEL","free_form")
   RecognizerIntent.PutExtra("android.speech.extra.PARTIAL_RESULTS",PARTIAL_RESULTS)
   RecognizerIntent.PutExtra("android.speech.extra.PREFER_OFFLINE",PREFER_OFFLINE)
   
   
   If nosound Then
       Dim p As Phone
       volA = p.GetVolume(p.VOLUME_ALARM)
       volM = p.GetVolume(p.VOLUME_MUSIC)
       volN = p.GetVolume(p.VOLUME_NOTIFICATION)
       volR = p.GetVolume(p.VOLUME_RING)
       volS = p.GetVolume(p.VOLUME_SYSTEM)
       volV = p.GetVolume(p.VOLUME_VOICE_CALL)
       
       p.setVolume(p.VOLUME_ALARM,           0,   False)
'       p.setVolume(p.VOLUME_MUSIC,           0,   False)  - comment out to not stop music from playing
       p.setVolume(p.VOLUME_NOTIFICATION,   0,   False)
       p.setVolume(p.VOLUME_RING,           0,   False)
       p.setVolume(p.VOLUME_SYSTEM,       0,   False)
       p.setVolume(p.VOLUME_VOICE_CALL,   0,   False)
   End If
   
   JO.RunMethod("startListening",Array(RecognizerIntent))
End Sub

A shame that we can't change the underling library to not do the beeps, because it works real well.
Music playing does not seem to effect the listening for commands
 

Brandsum

Well-Known Member
Licensed User
I dont think that you listen for voice input while playing something without the beep sound.
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
OK, but it seems to work fine. The playing sound is coming out the ear plug jack into a amp and playing over external speakers.
 

KZero

Active Member
Licensed User
Longtime User
language is always English , i tried ar-SA , it , fr

all returning english
 

JohnC

Expert
Licensed User
Longtime User
For the "LANGUAGE" parameter, if I specify a null ("") string, will this just set the preferred language of the results to the devices default language?

If not, how can I return results in the language of the user's device?
 

JohnC

Expert
Licensed User
Longtime User
Can an event be added to this library that will trigger when the device is fully ready and is now "listening" for speech?

(The BeginningofSpeech event is an event that triggers *after* someone starts talking - this is not what I am looking for)

What I am looking for is an event that triggers when the speech engine first starts to *listen*. I need such an event because on slower devices, especially when using the online mode, it may take a few seconds to fully initialize the SR engine before it's ready to start "hearing" words. So without this event (that I plan on displaying "Say Something" when this event triggers), the user may not know the SR engine is not yet listening and may miss the first couple of words the user said before it started to listen.
 

Brandsum

Well-Known Member
Licensed User
For the "LANGUAGE" parameter, if I specify a null ("") string, will this just set the preferred language of the results to the devices default language?
No

If not, how can I return results in the language of the user's device?

Here is how you can get the user's device language.
Get user's device language string:
Sub Get_Language As String
    Dim jo As JavaObject
    Return jo.InitializeStatic("java.util.Locale").RunMethod("getDefault", Null)
End Sub
 

Brandsum

Well-Known Member
Licensed User
Can an event be added to this library that will trigger when the device is fully ready and is now "listening" for speech?

(The BeginningofSpeech event is an event that triggers *after* someone starts talking - this is not what I am looking for)

What I am looking for is an event that triggers when the speech engine first starts to *listen*. I need such an event because on slower devices, especially when using the online mode, it may take a few seconds to fully initialize the SR engine before it's ready to start "hearing" words. So without this event (that I plan on displaying "Say Something" when this event triggers), the user may not know the SR engine is not yet listening and may miss the first couple of words the user said before it started to listen.
Yes. Its possible. I will post an update today.
 

JohnC

Expert
Licensed User
Longtime User
Yes. Its possible. I will post an update today.
Thank you for adding the "ReadyForSpeech" event so quickly - works great!
 

GeorgManfred

New Member
Licensed User
Many thanks for this.
I was looking for a way to use offline voice input to control my SmartHome center. Now I can do that well.
 

Ganiadi

Active Member
Licensed User
Longtime User
Hi, I got below error, pls help

Capture.PNG


tks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Top