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: 4,930

Chicao99

Member
Licensed User
I was testing this library and one time it returned a number in currency format (for example r$ 35,53), but it happened just one time, now it always returns the number extended (in portuguese is "trinta e cinco reais e cinquenta e três centavos", like (in english) "thirty five dolars and fifty three cents") , how can I choose it?
 

JTKEK

Member
Licensed User
can the Voice recognition work on background means, the pop of voice recognition is not shown
it is possible ?
 

tsteward

Well-Known Member
Licensed User
The idea of voice recognition appeals to me for my app. On a certain Activity I woul like to press the listen button only once.
Then listen for a command word. After a short pause process what was heard then do nothing but wait for that command word again. On closing this activity or pressing the button again it would stop listening.

How might I do this or is this beyond this library?
 

freedom2000

Well-Known Member
Licensed User
Hi,

I have successfully tested the voice recognition example.
However from time to time I have an error and a promt message saying (from french to english) :
"I have not understood, please retry"

And to retry I must push on the microphone icon.

Is there a way to automatically retry wihtout the message box ?

I would like to have an "everlasting" voice listener with voice recognition without user touching the screen...

Thanks
 

Syd Wright

Well-Known Member
Licensed User
Hi, Continuous voice recognition is not possible with Google's current API. I know all too well because this is something I am involved in for some years. It is necessary to use a push to talk touch button. You can however make sourcecode involving re-calling SR.startlistening as soon as the results from the previous dialog have been received (i.e. after the EndOfSpeech or PartialResults events). However, this will still cause pauses of about 2 seconds during which no speech is recognized.

There are currently 4 speech libraries described in this forum: VR by Google, XVerhelstX, VPires and Stevel05.
All lead to the same endresult. Nowadays recognition based on Googles technology is very good, provided the users speak clear and without too much background noise.

Since March Google has created a lot of problems with their service. Many developers have complained about this.
About a month ago Google finally resolved most problems, but still you will witness errors. The RMS event that can be used to drive a microphone level meter still is not working properly. It works only once after initiating the speech recognizer and then no more.

Google is working on a new Cloud based Speech Recognition API service, but this will be a paid service. The charge is 0.006 US$ per (standard) blocks of 15 seconds of speech. This tariff makes it unsuitable for continous speech recognition because the bill will be 20 US$ or more a day (1 hour = 240 blocks of 15 seconds = 1.44 US$).

My own solution is to use CMU PocketSphinx for offline recognition of a "hotword" (like "OK Harry") which then automatically triggers Google's speech recognition to process any subsequent spoken text. By chance I finalized my app today with this solution.
 

freedom2000

Well-Known Member
Licensed User
thank you for this clear answer.

How do you get the EndOfSpeech or PartialResults events ?

In the given exemple there is only : VR_Result (Success As Boolean, Texts As List)
 
Last edited:

freedom2000

Well-Known Member
Licensed User
You can however make sourcecode involving re-calling SR.startlistening as soon as the results from the previous dialog have been received (i.e. after the EndOfSpeech or PartialResults events). However, this will still cause pauses of about 2 seconds during which no speech is recognized.
.

Hi again !

I tried to relaunch the "startlistening" and it works (with a little delay) but when I get the "I have not understood, please retry" window, I need to push again the "micro button"
Any way to trap this event by code ?
 

Syd Wright

Well-Known Member
Licensed User
I recommend that you don't use the standard VR library but use the one by Vpires or Stevel05:
1: https://www.b4x.com/android/forum/threads/another-speechrecognizer-library.37584/#post-221995
2: https://www.b4x.com/android/forum/threads/android-speech-recognition-api-wrapper.62959/#content
3: https://www.b4x.com/android/forum/threads/chargeable-speechrecognizer-library.27409/
Number 3 (by XverhelstX) is chargable, but the maker is no longer active in this forum. Go for number 1!
The standard VR library (to my opinion) is somewhat outdated: it opens a new Window with a microphone, whereas the other three solutions can be better integrated into your project.
 

freedom2000

Well-Known Member
Licensed User
I recommend that you don't use the standard VR library but use the one by Vpires or Stevel05:
1: https://www.b4x.com/android/forum/threads/another-speechrecognizer-library.37584/#post-221995
2: https://www.b4x.com/android/forum/threads/android-speech-recognition-api-wrapper.62959/#content
3: https://www.b4x.com/android/forum/threads/chargeable-speechrecognizer-library.27409/
Number 3 (by XverhelstX) is chargable, but the maker is no longer active in this forum. Go for number 1!
The standard VR library (to my opinion) is somewhat outdated: it opens a new Window with a microphone, whereas the other three solutions can be better integrated into your project.

I tried "1" and it works great in fully auto mode

Thanks :)
 

petr4ppc

Well-Known Member
Licensed User
Dear friends,

have you experiences with conditions of using this voice recognition? As wrote "Syd Wright" - are here some parameters of free using of this function or we must pay something? 0-60 min free and 61-1000000minutes for 0,006 dolar per month and per 15 sec? If we use voice recognition in our app via B4A, we must pay or not?

Best regards
p4ppc
 
Top