Android Question SpeechRecognitionNoUI - Sub sr_Results executed more than once

magicmars

Member
Licensed User
Hi !

I'm new to B4A , this is my first post, and first I'd like to thank all the community of B4X :).

Thank you Brandsum for this nice libray.
I'm about to change the native Speech recognition by this one in my app !! :)

I've noticed that with the example given with the libray here , the Sub sr_Results is executed more than once.
I added logs to the begining of the sub and I can notice that its executed in that way :
if here is only one result in Texts , it is run once, while if there are more than 1 result in Texts, the Sub is executed twice.

B4X:
Sub sr_Results(Texts As List)
    Log("sr results")
    Dim Text As String = "Speech Results:"&CRLF&CRLF
    For Each t As String In Texts
        Text = Text & ">" & t & CRLF& CRLF
    Next
    Label1.Text = Text
End Sub

LOGS :

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
end of speech
sr results
sr results
end of speech
sr results
sr results
end of speech
sr results
sr results
end of speech
sr results
** Activity (main) Pause, UserClosed = false **

In my app, I use the sr_Results Sub to do another action that need only to be executed once per click on button, exactlty like in the app example.

How can I get the SR_Results Sub to be run only once ? (even if there a more than one results in the results, is can be kept in the list, I just want to use Texts.get(0) as the best match for my app).
I hope my request is enough easy to understand!
Thank you for your help an advice.
 

magicmars

Member
Licensed User
I solve my problem by storing the first texts.get(0) in a global variable, and by adding a simple condition that check if the text is the same as in the first run of the sr_results sub.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
The chances are you don't need to do that.

Just wait until the "end of speech" event is triggered, and the use the latest results in the texts.get(0).

Also I believe that library will keep looping the sr engine. So you may also want to execute sr.StopListening in the end of speech event too.
 
Upvote 0
Top