'Class module
'v.2 by Pomelov Vlad aka Peacemaker radioa@elec.ru
Sub Class_Globals
Private JO As JavaObject
Private RecognizerIntent As Intent
Private Initialized As Boolean
Public Busy As Boolean
Private SpeechRecognizer As JavaObject
Private Target As Object
Private Lang As String
Private SpeechRecognition_Name As String
End Sub
'Initializes the object. You can add parameters to this method if needed.
'ObjectName = name of this SpeechRecognition object
Public Sub Initialize(TargetModule As Object, RecognizeLanguage As String, ObjectName As String)
SpeechRecognizer.InitializeStatic("android.speech.SpeechRecognizer")
JO = SpeechRecognizer.RunMethod("createSpeechRecognizer",Array(JO.InitializeContext))
If Not(IsRecognitionAvailable) Then
Log("Speech Recognition Not Available")
ToastMessageShow("Speech Recognition is not Available", True)
Return
End If
SpeechRecognition_Name = ObjectName
Lang = RecognizeLanguage
Target = TargetModule
RecognizerIntent.Initialize("android.speech.action.VOICE_SEARCH_HANDS_FREE", "")
RecognizerIntent.PutExtra("calling_package",Application.PackageName)
RecognizerIntent.PutExtra("android.speech.extra.LANGUAGE_MODEL", "free_form")
RecognizerIntent.PutExtra("android.speech.extra.MAX_RESULTS",3)
RecognizerIntent.PutExtra("android.speech.extras.SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS", 1000)
RecognizerIntent.PutExtra("android.speech.extras.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS", 2000)
RecognizerIntent.PutExtra("android.speech.extras.SPEECH_INPUT_MINIMUM_LENGTH_MILLIS", 1000 * 10)
If Lang <> "" Then
RecognizerIntent.PutExtra("android.speech.extra.LANGUAGE", Lang)
End If
Dim Event As Object = JO.CreateEvent("android.speech.RecognitionListener","Received","")
JO.RunMethod("setRecognitionListener",Array(Event))
Initialized = True
End Sub
Public Sub IsInitialized As Boolean
Return Initialized
End Sub
Public Sub IsRecognitionAvailable As Boolean
Dim JO1 As JavaObject
JO1.InitializeContext
Return JO.RunMethod("isRecognitionAvailable",Array(JO1))
End Sub
Public Sub StartListening
Busy = True
JO.RunMethod("startListening",Array(RecognizerIntent))
End Sub
Public Sub StopListening
JO.RunMethod("stopListening",Null)
Busy = False
End Sub
Public Sub Destroy
JO.RunMethod("destroy",Null)
Busy = False
End Sub
Public Sub cancel
JO.RunMethod("cancel",Null)
Busy = False
End Sub
Private Sub Received_Event (MethodName As String, Args() As Object) As Object
Select MethodName
Case "onBeginningOfSpeech"
Case "onEndOfSpeech"
Busy = False
If SubExists(Target, SpeechRecognition_Name & "_onEndOfSpeech") Then
CallSubDelayed(Target, SpeechRecognition_Name & "_onEndOfSpeech")
End If
Case "onError"
Busy = False
'Dim ErrorMsg As String = GetErrorText(Args(0))
If SubExists(Target, SpeechRecognition_Name & "_onError") Then
CallSubDelayed2(Target, SpeechRecognition_Name & "_onError", Args(0))
End If
Case "onResults"
Busy = False
Dim Results As JavaObject = Args(0)
Dim Matches As List = Results.RunMethod("getStringArrayList",Array("results_recognition"))
If SubExists(Target, SpeechRecognition_Name & "_onResults") Then
CallSubDelayed2(Target, SpeechRecognition_Name & "_onResults", Matches)
End If
Case "onRmsChanged"
Busy = True
If SubExists(Target, SpeechRecognition_Name & "_onRmsChanged") Then
CallSubDelayed2(Target, SpeechRecognition_Name & "_onRmsChanged", Args(0))
End If
End Select
End Sub
Sub GetErrorText(ErrorCode As Int) As String
Select ErrorCode
Case SpeechRecognizer.GetField("ERROR_AUDIO")
Return "Audio Recording error"
Case SpeechRecognizer.GetField("ERROR_CLIENT")
Return "Client side error"
Case SpeechRecognizer.GetField("ERROR_INSUFFICIENT_PERMISSIONS")
Return "Insufficient permissions"
Case SpeechRecognizer.GetField("ERROR_NETWORK")
Return "Network error"
Case SpeechRecognizer.GetField("ERROR_NETWORK_TIMEOUT")
Return "Network timeout"
Case SpeechRecognizer.GetField("ERROR_NO_MATCH")
Return "No match"
Case SpeechRecognizer.GetField("ERROR_RECOGNIZER_BUSY")
Return "RecognitionService busy"
Case SpeechRecognizer.GetField("ERROR_SERVER")
Return "error from server"
Case SpeechRecognizer.GetField("ERROR_SPEECH_TIMEOUT")
Return "No speech input"
Case Else
Return "Didn't understand, please try again."
End Select
End Sub