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,251

Beja

Expert
Licensed User
Longtime User
Hi Erel,
Today I tried the example on the first page and it was running.. but the tts was slower than the voice reco
eg. if you say "hello how are you", the TTS said "are you" or the last words.. Not sure if this issue was raised in the previous pages because I didn't read them all.
I added a sleep function and the TTS played be the full recognized text.

B4X:
Sub VR_Result (Success As Boolean, Texts As List)
    If Success = True Then
        ToastMessageShow(Texts.Get(0), True)
        '____________
        Sleep(1000)
       '-------------
        TTS1.Speak(Texts.Get(0), True)
    End If
End Sub
 
Top