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:
Thanks in advance for your time
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