Android Question Start sub on voice command. CRGoogVR-Library

stp

Active Member
Licensed User
Longtime User
I am using that code and try to find a solution.
code:
#Region  Project Attributes
    #ApplicationLabel: CRGoogVR Example
    #VersionCode: 1
    #VersionName: 1.0
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
    #BridgeLogger: True
#End Region

Sub Process_Globals
End Sub

Sub Globals
    Dim CRGVR As CRGoogVR
    Dim bnStart, bnStop As Button
    Dim edText As EditText
    Dim pnCmd As Panel
    Dim lbStatus As Label
    Dim ivRMS As ImageView
    Dim vu00, vu20, vu40, vu60, vu80, vu90 As Bitmap
    Dim rp As RuntimePermissions
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
 
    pnCmd.Width = 90%x
    pnCmd.Left = 5%x
    pnCmd.Top = 5dip
 
    Dim spc As Int = (pnCmd.Width - (bnStart.Width + bnStop.Width)) / 3
    bnStart.Left = spc
    bnStop.Left = bnStart.Left + bnStart.Width + spc
 
    bnStart.Enabled = True
    bnStop.Enabled = False
 
    spc = (pnCmd.Width - (lbStatus.Width + ivRMS.Width)) / 3
    lbStatus.Left = spc
    ivRMS.Left = lbStatus.Left + lbStatus.Width + spc
 
    edText.SetLayout(pnCmd.Left, pnCmd.Height, pnCmd.Width, 100%y - pnCmd.Height)
 
    edText.Text = ""
    edText.Enabled = False
 
    vu00 = LoadBitmap(File.DirAssets, "vu00.png")
    vu20 = LoadBitmap(File.DirAssets, "vu20.png")
    vu40 = LoadBitmap(File.DirAssets, "vu40.png")
    vu60 = LoadBitmap(File.DirAssets, "vu60.png")
    vu80 = LoadBitmap(File.DirAssets, "vu80.png")
    vu90 = LoadBitmap(File.DirAssets, "vu90.png")
 
    Log(CRGVR.Identify)
    RequestPermissions
End Sub

Sub Activity_Resume
    ' You need to initialize the CRGoogVR library passing the Event Name Prefix string:
 
    CRGVR.Initialize("CRGVR")
 
    UpdStatus(False)
    UpdVU(0)
End Sub

Sub Activity_Pause(UserClosed As Boolean)
    CRGVR.DestroyVR

End Sub

Sub AddText(txt As String)
    If (edText.Text <> "") Then edText.Text = edText.Text & CRLF
    
    edText.Text = edText.Text & txt

    edText.SelectionStart = edText.Text.Length
    edText.RequestFocus
End Sub

Sub UpdStatus(flg As Boolean)
    If (flg) Then
        lbStatus.TextColor = Colors.Green
        lbStatus.Text = "recording"
    Else
        lbStatus.TextColor = Colors.Red
        lbStatus.Text = "not recording"
    End If
End Sub

Sub UpdVU(rms As Float)
    If (rms >= 9) Then
        ivRMS.Bitmap = vu90
    Else If (rms >= 8) Then
        ivRMS.Bitmap = vu80
    Else If (rms >= 6) Then
        ivRMS.Bitmap = vu60
    Else If (rms >= 4) Then
        ivRMS.Bitmap = vu40
    Else If (rms >= 2) Then
        ivRMS.Bitmap = vu20
    Else
        ivRMS.Bitmap = vu00
    End If
End Sub

Sub bnStart_Click
    CRGVR.StartVR
End Sub

Sub bnStop_Click
    CRGVR.StopVR
End Sub


' *** CRGoogVR Events ***

' PartialResult(Result As String)
' FinalResult(Result As String)
' MicReady(Ready As Boolean)
' BeginSpeech(Begin As Boolean)
' EndSpeech(End As Boolean)
' AudioRMS(Rms As Float)
' Error(Error As Int)
' Event(Event As Int)
' LogMsg(Msg As String)"})

Sub CRGVR_PartialResult(Res As String) ' CRGoogVR library received partial result
    '  Log("Partial Result")
    '  Log("  " & Res)

'    If (Res.Trim <> "") Then AddText("PR: " & Res)
    If (Res.Trim <> "") Then Log(Res)
End Sub

Sub CRGVR_FinalResult(Res As String) ' CRGoogVR library received final result
    '  Log("Final Result")
    '  Log("  " & Res)

    If (Res.Trim <> "") Then AddText("FR: " & Res)
End Sub

Sub CRGVR_MicReady(rdy As Boolean) ' CRGoogVR mic is ready for recording
    bnStart.Enabled = False
    bnStop.Enabled = True

    UpdStatus(True)
End Sub

Sub CRGVR_BeginSpeech(bsp As Boolean) ' CRGoogVR start of speech detected
End Sub

Sub CRGVR_EndSpeech(esp As Boolean) ' CRGoogVR end of speech detected
    bnStart.Enabled = True
    bnStop.Enabled = False

    UpdStatus(False)
End Sub

Sub CRGVR_AudioRMS(rms As Float) ' CRGoogVR internal log message
    '  Log("RMS: " & rms)
 
    UpdVU(rms)
End Sub

' 3 - ERROR_AUDIO - Audio recording error.
' 5 - ERROR_CLIENT - Other client side errors.
' 9 - ERROR_INSUFFICIENT_PERMISSIONS - Insufficient permissions
' 2 - ERROR_NETWORK - Other network related errors.
' 1 - ERROR_NETWORK_TIMEOUT - Network operation timed out.
' 7 - ERROR_NO_MATCH - No recognition result matched.
' 8 - ERROR_RECOGNIZER_BUSY - RecognitionService busy.
' 4 - ERROR_SERVER - Server sends error status.
' 6 - ERROR_SPEECH_TIMEOUT - No speech input

Sub CRGVR_Error(err As Int) ' CRGoogVR Error code
    Log("CRGVR Error: " & err)
End Sub

Sub CRGVR_Event(evt As Int) ' CRGoogVR Event received (for future extensions)
    Log("CRGVR Event: " & evt)
End Sub

Sub CRGVR_LogMsg(msg As String) ' CRGoogVR internal log message
    Log(msg)
End Sub

Sub RequestPermissions
    ToastMessageShow("Requesting permissions", False)
    rp.CheckAndRequest(rp.PERMISSION_RECORD_AUDIO)
End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    If Permission = rp.PERMISSION_RECORD_AUDIO Then
        CRGVR.Initialize("CRGVR")
    End If
End Sub

I got the code from testcode and added RequestPermissions. My question: Is it possible to start listening with a voice command? Eg "Start"
Now i have to click on the button to do that.
Thank you
 
Top