Android Question Speech Recognition with JavaObject + Reflection

Nerdworld

Member
Licensed User
Longtime User
Hello!

I'm trying to use the Speech Recognition API via JavaObjects and Reflection library.
After some hours of research, i finally made (mostly) working version.

This is my Service:

B4X:
#Region  Service Attributes
    #StartAtBoot: True
#End Region

Sub Process_Globals
    Dim R As Reflector
    Dim SpeechObj As JavaObject
    Dim RecognizeIntent As Intent
End Sub

Sub Service_Create
End Sub

Sub Service_Start(StartingIntent As Intent)
    RecognizeIntent.Initialize("android.speech.action.ACTION_RECOGNIZE_SPEECH", "")
    '
    SpeechObj.InitializeStatic("android.speech.SpeechRecognizer")
    SpeechObj = SpeechObj.RunMethod("createSpeechRecognizer", Array As Object(R.GetContext))
    '
    Dim SpeechEvent As Object = SpeechObj.CreateEvent("android.speech.RecognitionListener", "SpeechRecognition", False)
    SpeechObj.RunMethod("setRecognitionListener", Array As Object(SpeechEvent))
End Sub

Sub Service_Destroy
End Sub

Sub Listen()
    SpeechObj.RunMethod("startListening", Array As Object(RecognizeIntent))
End Sub

Sub SpeechRecognition_Event(Name As String, Args() As Object)
    If (Name <> "onRmsChanged") Then
        Log("SpeechRecognition Event: " & Name)
    End If
    Select (Name)
        Case "onResults"
            Dim Obj As JavaObject = Args(0)
            Obj = Obj.RunMethod("getStringArrayList", Array As Object("results_recognition"))
            '
            Dim Lines As String = "mobile|voice"
            For i = 0 To Obj.RunMethod("size", Array As Object()) - 1
                Dim Line As String = Obj.RunMethod("get", Array As Object(i))
                Lines = Lines & "|" & Line
            Next
            Log(Lines)
        Case "onError", "onEndOfSpeech"
            ' Speech ended
        Case "onRmsChanged"
            ' Args(0) = Sound level
    End Select
End Sub

The Service is created once the application is started, the Listen event might be called by a button or a TCP message. But now, the problem:

When i first wrote this code (in the Main activity, not in a service) everything worked really fast - but in the service module the events: 1. getting dispatched very slow, sometimes there is a delay of about 2-3 seconds and 2. some events won't get dispatched - but only sometimes. In example the onError/onEndOfSpeech/onResults event.

Is there a problem with my code, or with the service?
Is there a way to enhance the speed of this service?
 

Nerdworld

Member
Licensed User
Longtime User
The slowdown is what i could live with - but sometimes the events are not registered - there is a "start" event, but no "end" event.

Looks like the SpeechRecognizer methods might only be called from the main thread:
http://developer.android.com/reference/android/speech/SpeechRecognizer.html said:
This class's methods must be invoked only from the main application thread.

Searching for a workaround.
 
Last edited:
Upvote 0
Top