Android Question VoiceRecognizer

QtechLab

Active Member
Licensed User
Hello everyone,

I'm trying to create a class that can listen and recognize vocal commands with a reference list.
I looked for a solution in this forum but i'm having some problem to get this app works.

I'm testing this on a Nexus5X with Android 8.0 Oreo Preview for developer.

When i want to listen for a command i call the function "Recongizer.Listen" but i'm not having a intent of the current operation, i tryed with the Listen2(VocalIntent) but nothing. Can you have a look of the code and give me a little help?
I didn't add any string in the manifest editor.

This is the class:
B4X:
Sub Class_Globals
   
    Private Recognitor As VoiceRecognition
    Private Speacher As TTS
   
    Private Listening As Boolean = False
    Private CommandList As List
    Private VocalList As List
    Private VocalResponse As List
   
    Dim VocalIntent As Intent
   
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
   
    Recognitor.Initialize("Recognitor")
    Recognitor.Language = "it"
    Recognitor.Prompt = "Attesa comando"
   
    VocalIntent.Initialize(VocalIntent.ACTION_MAIN, "")
    VocalIntent.SetComponent("com.google.android.googlequicksearchbox/com.google.android.voicesearch.VoiceSearchPreferences")
    VocalIntent.Flags = 268435456    'FLAG_ACTIVITY_NEW_TASK
   
    If Recognitor.IsSupported Then
        Speacher.Initialize("Speacher")
        Speacher.SetLanguage("it", "IT")
       
        CommandList.Initialize
        VocalList.Initialize
        VocalResponse.Initialize
       
        AddCommandsToList
    Else
        Log("VoiceRecognition not supported")
    End If
End Sub

Private Sub AddCommandsToList
    CommandList.Add(1)
    CommandList.Add(2)
    Dim j As Int = 0
   
    For J = 0 To CommandList.Size -1
        Select CommandList.Get(j)
           
            Case 0               
                VocalList.Add(Array("Accendi", "Accensione", "Attiva","Attivazione", "Arma"))
                VocalResponse.Add("Accensione eseguita")
               
            Case 1
                VocalList.Add(Array("Spegni", "Spegnimento", "Disattiva","Disattivazione", "Disarma"))
                VocalResponse.Add("Spegnimento eseguito")
               
        End Select
    Next
   
End Sub

'Function called by activity to start listening
public Sub GetListen
    If Listening = False Then
        Listening = True
        'Recognitor.Listen2(VocalIntent)
        Recognitor.Listen
    End If
End Sub

private Sub Recognitor_Result (Success As Boolean, Texts As List)
    If Success = True Then
        Dim comando As Int = RecognizeCommand(Texts)
        If comando < 255 Then
            Dim Sentence As String = VocalResponse.Get(comando)
            Speacher.Speak(Sentence, True)
        Else
            Speacher.Speak("Comando non disponibile", True)
        End If
    Else
        Speacher.Speak("Non ho capito", True)
    End If
    Listening = False
End Sub

'Recognize commands in the lists
private Sub RecognizeCommand(texts As List) As Int
    Dim retVal As Int = 255
    Dim NumWord As Int = 0
    Dim CommandFound As Boolean = False
   
    Do While NumWord < texts.size -1 And Not(CommandFound)
        Dim word As String = texts.Get(NumWord)
        Dim j As Int = 0
        Do While j < VocalList.Size -1 And Not(CommandFound)
            Dim accepted() As String = VocalList.Get(j)
            For Each str As String In accepted
                 If str = word Then
                    CommandFound = True
                    retVal = j
                End If
            Next
            j = j + 1
        Loop
        NumWord = NumWord +1
    Loop
   
    Return retVal
End Sub

Thanks in advance for your time
 

Erel

B4X founder
Staff member
Licensed User
Are you initializing this class from an Activity code?

I tested it with this code and it works:
B4X:
'class c1
Sub Class_Globals
   Private vr As VoiceRecognition
End Sub

Public Sub Initialize
   vr.Initialize("vr")
End Sub

Public Sub Test
   vr.Listen
   Wait For vr_Result (Success As Boolean, Texts As List)
   Log(Texts)
End Sub

'main:
Sub Process_Globals
   Private cc1 As c1
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   If FirstTime Then
     cc1.Initialize
   End If
End Sub

Sub Activity_Click
   cc1.Test
End Sub
 
Upvote 0

QtechLab

Active Member
Licensed User
This class is initialized in the Starter service, i'll use it in every activity of my code. Are you saying that i should use a single class instance per activity?

Thanks for reply.
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
This class is initialized in the Starter service, i'll use it in every activity of my code. Are you saying that i should use a single class instance per activity?

Thanks for reply.
I create my instances of classes in starter then initialise them where I need to
 
Upvote 0
Top