Android Tutorial Voice Recognition Example

Here's a quick example of voice recognition.
It just displays the results to the log window.
This example shows all the different recognized patterns that are returned by the Voice Module, usually 4 or 5 possibilities.

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
Dim VR As VoiceRecognition

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
VR.Initialize("VR")
VR.Listen

End Sub
Sub VR_Result (Success As Boolean, Texts As List)
   If Success = True Then
   For i = 0 To Texts.Size -1
   Log (Texts.Get(i))
   Next
   End If   
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Last edited:

mcmanu

Active Member
Licensed User
Longtime User
Hi

Where i can find the Phone Library?
and how to use it so that i can use Voicerecognition? sorry because of my english ;)
 

klaus

Expert
Licensed User
Longtime User
The phone library is part of Basic4Android.
When you run the IDE, you have in the lower right corner a tab called Lib.
There you should see the Phone library, check it and it should work.
The latest version of the library is 1.6

Best regards.
 

mcmanu

Active Member
Licensed User
Longtime User
hi

Oh okay thanks ;) iam blind xD
A i love basic4android its so simple to use ;)
 

mcmanu

Active Member
Licensed User
Longtime User
Another Question ;)

I have another question, i have now Programmed that the voicereg. Listens to me when i click a button. Now i want it to write what i was saying into the edittext, how can i do that?
 

mcmanu

Active Member
Licensed User
Longtime User
Thanks

Oh okay so easy Thanks ;)
yes i have recognized that in the forum are a lots of examples ;)
Thanks a lot ;)
 

Askjerry

Member
Licensed User
Longtime User
:sign0098:

I was hoping to be able to do something with voice recognition... I simply cannot believe how easy it was! AWESOME. Here is my code... you click a button and it drops it into a label.

Why a label? Because I didn't want the user to edit it... just display it... and it works very nicely!!!

B4X:
Sub Button1_Click
Dim VR As VoiceRecognition
   VR.Initialize("VR")
   VR.Language="en"
   VR.Prompt="Talk to me!"
   VR.Listen
   ' If it workes it will jump to VR_Result
End Sub

Sub VR_Result (Success As Boolean, Texts As List)
    If Success = True Then
        Label1.Text = Texts.Get(0)
    End If
End Sub

I originally had the DIM in the initial setup... but then it didn't work... so I moved it to the button click area... then it was happy.

Anyway... thanks again!
Jerry
 
Last edited:

marcocim

Member
Licensed User
Longtime User
B4A-HelpViewer crash

My B4A-HelpViewer.exe crashes on startup with an 'OfflineHelper stops to run'.

Thanks

Marco
 

skywalker34

Member
Licensed User
Longtime User
Hi Erel,

I have done some good job from all example, thanks to you and all that share their knowledge

In a project I want to use interectiv with TTS and voice recognition

If I play a message using TTS I have to wait certain time before using VR.listen else the VR get my TTS sentence.

So I coded something like
TTS1.speak (my_sentence,true)
do while timer_delay<2000
doevents
loop
VR.listen

1/ Is there a way to know where then TTS.speak has finished it job better than having a loop with delay, because this needed delay varie depending on the sentence to pronounce as the voice synthesizer parameters ?

2/ Where can I found the differents events definition corresponding to TTS (or other)like the TTS_ready (success as boolean) ?

Rgds

Sky
 

skywalker34

Member
Licensed User
Longtime User
2) TTS service sends an intent when it finishes. You can use PhoneEvents to handle the TextToSpeechFinish event.

thanks for those info, I am still beginner in B4A :( , could you please detail and give sample code on the way to handle this phoneEvents ?

Rgds

Sky
 

skywalker34

Member
Licensed User
Longtime User
As I made lots of exchange with the user I wrote a sub with

TTS1.speak (my_sentence,true)
do while timer_delay<2000
doevents
loop
VR.listen

that I can call each time I need to ask something and get answer
The problem is that after several exchange with the user the TTS.speak don't speek anymore :confused:

Any idea of what happen ?
 
Top