B4A Library Another SpeechRecognizer Library

Sample code

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim sr As SpeechToText
    Dim ri As RecognizerIntent
    Dim i As Intent
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim EditText1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
   
    EditText1.Initialize("")
    EditText1.SingleLine=False
    EditText1.Gravity=Gravity.TOP
    Activity.AddView(EditText1,0,0,100%x,100%y)
 
  
    sr.initialize("sr")
    i.Initialize( ri.ACTION_VOICE_SEARCH_HANDS_FREE,"")
    i.PutExtra(ri.EXTRA_CALLING_PACKAGE,"com.inforpires.testSpeech")
    i.PutExtra(ri.EXTRA_LANGUAGE_MODEL, ri.LANGUAGE_MODEL_FREE_FORM)
    'i.PutExtra(ri.EXTRA_LANGUAGE,"pt_PT")
    i.PutExtra(ri.EXTRA_MAX_RESULTS,3)
    i.PutExtra(ri.EXTRA_PARTIAL_RESULTS,True)
  
End Sub
Sub sr_onResults(results As Object)
  
    Log(results)
    Dim words As List
    'Log(words)
    words.Initialize2(results)
    For Each word As String In words
        Log(word)
        EditText1.Text = EditText1.Text & word
    Next
    EditText1.Text = EditText1.Text & CRLF
  
    sr.startListening(i)
End Sub
Sub sr_onError(erro As Int)

    Log("ERROR : " & erro)
    If erro=8 Then
        sr.stopListening
        sr.destroy
        sr.createSpeechRecognizer
        sr.startListening(i)
    Else
        sr.startListening(i)
    End If  
End Sub

Sub sr_onEndOfSpeech
    Log("End of speech")
    'sr.startListening(i)
End Sub
Sub sr_onRmsChanged(rmsdB As Float)
    'Log("RMS" & rmsdB)
End Sub
Sub Activity_Resume
    If sr.isRecognitionAvailable Then
              
        sr.createSpeechRecognizer
        sr.startListening(i)
        Log("OK")
    Else
        Log("Not OK")
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    sr.destroy
End Sub

Unzip speech2TextLib.zip into extralibs folder & provide results reports, please.

Nelson Pires
 

Attachments

  • speech2TextLib.zip
    5.1 KB · Views: 791
Last edited:

peacemaker

Expert
Licensed User
Strange, lib works OK (online and offline, if offline-language is downloaded) in debug-mode. But when to compile at last the release (or obfuscated release - the same) - no recognition. Who knows what the issue is ?
 
Last edited:

Chris160179

Member
Licensed User
Hallo @all,

would it be possible to add the intent "EXTRA_PREFER_OFFLINE" to this Library?
Or is there a way to edit the library?

At this Moment the intent is not supportet with this library.

Thx an have a nice Sunday
 

JackKirk

Well-Known Member
Licensed User
Hi,

I am developing an app that, amongst other things, needs to take a photo with the camera while the phone is held very steady.

Tapping the screen to take the photo doesn't allow you to keep phone very steady.

I have already developed an alternate mechanism to take the photo which involves measuring "steadiness" via the accelerometer/magnetometer and a time delay. This works but is not perfect.

I am experimenting with this library with a view to adding a simple voice activation mechanism to take the photo.

Initial experiments are encouraging with one small issue.

I can rig my phone (Samsung S5) so that voice recognition with this library will work offline (in flight mode).

When I take flight mode off it reacts somewhat slower so it is obviously using a remote service - this is not desirable.

I tried fiddling with the library's .xml to add EXTRA_PREFER_OFFLINE but that just raised a compile time error - so obviously something needs to be changed in the library's .jar (this sentence spells out my total lack of expertise in this area).

So I totally support Chris160179's post above.

Thanks in advance...
 

JackKirk

Well-Known Member
Licensed User
Further to the previous post...

It turns out that this library is basically totally eclipsed by the class published by stevel05 at:

https://www.b4x.com/android/forum/threads/android-speech-recognition-api-wrapper.62959/

I was able to take that class, tidy it up for my needs and add:

RecognizerIntent.PutExtra("android.speech.extra.PREFER_OFFLINE", True)

in the Initialize method.

This gave me offline voice recognition.

Details of all of the RecognizerIntent constants ("android.speech.extra.PREFER_OFFLINE" etc) are available in the bottom part of this page:

https://developer.android.com/reference/android/speech/RecognizerIntent.html

Happy coding...
 

ddk1

Member
Licensed User
Thanks for the great bit of code vpires.
Its 2021, I'm on Android 11, and I've been using the VoiceRecognition class, part of Phone. But I don't like the way it pops up a 'Speak now' dialog, and worse, if I don't speak quick enough it make me have to click a link to get it listening again. Not so good when driving.
So I tried your code and it works brilliantly and doesn't force any dialog on me, I can choose what sort of prompt I might want to show the user.
For anyone else trying this, I found it kept giving error 9, no permission.
I used RuntimePermissions class to obtain permission PERMISSION_RECORD_AUDIO, at runtime.
I didn't need to do any of what Alberto Iglesias did, obtaining permission CHANGE_CONFIGURATION via ADB.
Many thanks
 
Top