Android Tutorial Voice Recognition Example

A large button is displayed. When the user presses on the button, the user is asked to say something.
The voice recognition engine converts the audio to text.

Then the text is converted back to speech using the TTS (text to speech) library:
B4X:
'Activity module
Sub Process_Globals
    Dim VR As VoiceRecognition
    Dim TTS1 As TTS
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        VR.Initialize("VR")
        TTS1.Initialize("TTS1")
    End If
    Activity.LoadLayout("1")
    If VR.IsSupported Then
        ToastMessageShow("Voice recognition is supported.", False)
    Else
        ToastMessageShow("Voice recognition is not supported.", True)
    End If
    VR.Prompt = "Say your message"
End Sub

Sub Button1_Click
    VR.Listen 'calls the voice recognition external activity. Result event will be raised.
End Sub

Sub VR_Result (Success As Boolean, Texts As List)
    If Success = True Then
        ToastMessageShow(Texts.Get(0), True)
        TTS1.Speak(Texts.Get(0), True)
    End If
End Sub
VR is a VoiceRecognition object. We call its Listen method. This method launches the external voice recognition application. When the result is ready the Result event is raised.
We need to check the Success flag to make sure that there is at least one text available.
The Texts list holds all the possible results. We take the supposedly best one which is the first.
The program is attached.
 

Attachments

  • VoiceRecognition.zip
    5.6 KB · Views: 5,571

susu

Well-Known Member
Licensed User
Longtime User
This example said "Voice recognition is not supported" although I installed TTS library on my device already (my app can read English). What's wrong?
 

susu

Well-Known Member
Licensed User
Longtime User
Yes, my phone got Voice Dialler (stock app). I can use it to dial by say the number.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Sorry but it seems like your phone doesn't have the required application that handles the voice recognition. Maybe you can install such an application. It should be able to handle the "android.speech.action.RECOGNIZE_SPEECH" intent.

You can try changing the Language property to "en":
VR.Language = "en"
Ignore the not supported message and see if it returns any result.
 

susu

Well-Known Member
Licensed User
Longtime User
I tried: VR.Language = "en" but it still not work. Looking for some VR apps on Market and wish it will help.
 

Libor

Member
Licensed User
Longtime User
Hi Erel,

Is there any way to programaticaly check if the particular phone/device supports the needed functionality?

I'm thinking to disable this feature if it's not available.

I'm putting together a kind of language learning application based on the idea of flash cards but with also sound and some animations. It would be great to add voice recognition.

thanks

Libor
 

DKnowles

Member
Licensed User
Longtime User
Voice Recognition 'Speak Now' Box disappears straight away

Hi Experts

Just about to add Voice Recognition into an application, but when I run the example (both Erel's and Jamie's) , the Android Speck now box appears but only stays on the screen for about 1 second, if I shout at phone the microphone lights up so i know the system can hear me ok, any ideas.

I am not getting the _Result sub called at all in ether example.

Note the Voice Recognition works with other application e.g. sat nav etc

Help

David
 

nachoap10

Member
Licensed User
Longtime User
Hi!

I can't compile the example. I get this error:

Error description: Unknown type: voicerecognition

Should I include a new lib?

I've compiled it with 1.6 and 1.7, with the same results. I've checked all standard lib marks, and I got the same error. And I can't find a VoiceRecognition lib in lib posts (I've assumed Recognition is in Phone lib). :BangHead:

Please, could you tell me what I have done wrong?

Thank you in advance.
 

nachoap10

Member
Licensed User
Longtime User
Thank you, Erel. It was a WRONG error mine. I supossed 1.7 will use his phone lib, and I forgot I had got other older lib in my personal lib folder, and b4a was using this phone lib (1.20).

:sign0013:

Now everything works really fine. I've tried it (speaking in spanish, because my oral English is still worse as my written English :signOops:) and it works perfectly (in fact, I've been surprised by how it recognises words :icon_clap:. It has only failed with "supercalifragilistiuespialidoso", from Mary Poppins :sign0012:)
 

vb1992

Well-Known Member
Licensed User
Longtime User
Erel,

How do you get to the "text" of what the user said?
after he speaks into the microphone and the voice recognition
displays it (ToastMessageShow(Texts.Get(0), True)), but I can't seem to grab what he said and use it (the text) myself

Can't seem to figure that out..
In case I want to save them to a database.
 

ChrShe

Member
Licensed User
Longtime User
VR with no prompt...

I have a need to use the Voice Recognition, and so far it's working great! Except that my wife has to over-enunciate. LOL!

Now, I'd like to have my app listen until it hears a specific word or sentence, but I don't want the "Speak now" dialog sitting on top of my app.
Is there a way to hide, or turn off the prompt and have VR continuously listen?

Thanks!

EDIT:
In playing with this more, I have another question.
I found that my app was trying to match what it heard on the Tele. LOL
Now can I programmatically dismiss the "No matches found" dialog from Result = False and return to listening?
 
Last edited:

netchicken

Active Member
Licensed User
Longtime User
Erel, I havn't played with this yet, although I am really looking forward to it, but why use a list? is a string not suitable?

Texts is a List object that holds all the possible options. You will usually want to get the first item:
B4X:
Dim UserText As String
UserText = Texts.Get(0)
 

ChrShe

Member
Licensed User
Longtime User
From the little bit that I've played with it, the Voice Recognition service returns a list of possible matches.

I.e. I say "Where's my phone?" What gets returned is:
- where is my phone
- where's my phone
- where's my home
- where is my bone
- where is my comb
Etc.

I don't know for sure, but I suspect that the list is ranked in order of probable matches.

-Chris
Sent from my DROID RAZR using Tapatalk 2
 

CarlM

Member
Licensed User
Longtime User
Is there any way to control the length of time that the VR listens for?
Ideally I would like it to listen until I hit a 'Stop' button - which could be up to 30 seconds?


Edit: Just realised it listens until theres a break in speech
 
Last edited:
Top