iOS Question Speech recognition + phone vibration

HuZz

Member
Licensed User
Hi everybody,
it's weird but i'm facing this problem, and dunno if it's a bug or not:
I'm using speech recognition in my app and i would like the phone to vibrate anytime an event is called by the speech_result sub... but this doesn't happen!
so i added a button which enable/disable speech.recording... if speech recording is off then the phone vibrate, but the phone do nothing while speech recording is on!
Is this normal?

thanks
Davide
 

HuZz

Member
Licensed User
Here it is a small example in which the app should vibrate after my sub is called...
when in debug mode it comes to the vibration call, but it passes over without doing anything...

B4X:
#Region  Project Attributes
    #ApplicationLabel: tstSpeech
    #Version: 1.0.0
    'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
    #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
    #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
    #Target: iPhone, iPad
    #ATSEnabled: True
    #MinVersion: 10
    #PlistExtra:<key>NSSpeechRecognitionUsageDescription</key><string>More information here...</string>
    #PlistExtra:<key>NSMicrophoneUsageDescription</key><string>Speech recognition</string>
#End Region   

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page

    Dim speech As SpeechRecognition
    Dim myPhone As Phone
    Dim tmr As Timer
   
End Sub

Private Sub Application_Start (Nav As NavigationController)
    'SetDebugAutoFlushLogs(True) 'Uncomment if program crashes before all logs are printed.
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"
    Page1.RootPanel.Color = Colors.White
    NavControl.ShowPage(Page1)
   
    speech.Initialize("speech")
    'awaits 100 millisecs before start recording
    tmr.Initialize("tmr",100)
    tmr.Enabled = True
End Sub

Private Sub tmr_Tick   
    speech.StartRecording(True)   
    tmr.Enabled = False
End Sub

Private Sub  speech_Result (Success As Boolean, IsFinal As Boolean, Texts As List)
    If Success Then   
        Log(Texts.get(0))
        'example of called sub
        doVibrate   
    Else           
    End If
End Sub

Private Sub doVibrate
    Sleep(100)
    myPhone.Vibrate
End Sub

Private Sub     speech_AuthorizationStatusChanged (Status As Int)
    If speech.IsAuthorized Then
        Dim lang As String = "en"
        If speech.SetLanguage(lang) = False Then
            Log("Speech Recognition not available.")
        Else
            Log("ready")
        End If
    Else
        Log("Not authorized...")
    End If
End Sub
 
Upvote 0

HuZz

Member
Licensed User
Finally solved in some way... dunno if this can be a useful solution for somebody else (comments appreciated)
i added 2 timers
B4X:
Private Sub tmrSpeech_Tick   
    speech.StartRecording(True)   
    tmrSpeech.Enabled = False
End Sub
Private Sub tmrVibration_Tick
    speech.StopRecording
    doVibrate
    tmrVibration.Enabled = False
    tmrSpeech.Enabled = True
End Sub
Private Sub doVibrate
    myPhone.Vibrate   
End Sub

In speech_Result sub, if Success, i set tmrVibration.enabled = true
In this way i'm able to get phone vibration and immediately after start recording again...

If someone has a better solution, please post it here
thanks

Davide
 
Upvote 0

HuZz

Member
Licensed User
I’m sorry, but here i am again with updates... my solution doesn’t work in my real project... it seems to work in the small example, but not in the app i’m developing!
Is it possibile i cannot make my phone vibrate togheter with Speech recognition??
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Don't handle partial results. The vibrate will not work while audio is recorded.

Correct code:
B4X:
Private Sub Application_Start (Nav As NavigationController)
   'SetDebugAutoFlushLogs(True) 'Uncomment if program crashes before all logs are printed.
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.Title = "Page 1"
   Page1.RootPanel.Color = Colors.White
   NavControl.ShowPage(Page1)
   speech.Initialize("speech")
End Sub

Private Sub speech_AuthorizationStatusChanged (Status As Int)
   If speech.IsAuthorized Then
     speech.SetLanguage("en")
     Log("ready")
   Else
     Log("Not authorized...")
   End If
End Sub

Sub Page1_Touch(Action As Int, X As Float, Y As Float)
   If Action = Page1.RootPanel.ACTION_DOWN Then
     speech.StartRecording(False)
     Page1.RootPanel.Color = Colors.Green
   Else if Action = Page1.RootPanel.ACTION_UP Then
     speech.StopRecording
     Page1.RootPanel.Color = Colors.White
     Wait For speech_Result (Success As Boolean, IsFinal As Boolean, Texts As List)
     If Success Then
       Log(Texts.get(0))
       'example of called sub
       doVibrate
     End If
   End If
End Sub


Private Sub doVibrate
   Sleep(100)
   myPhone.Vibrate
End Sub
 
Upvote 0

HuZz

Member
Licensed User
Thanks Erel for your answer, but this way i'm outside my purpouse.
My app is a stopwatch with speech recognition.... so the user don't have to push a button to start/stop recording. That's why i was handling partial results.
The app works correctly but i was thinking to add vibration or some kind of sound in order to make the user know his vocal command has been correctly received.
If yours is the only solution, i'll work on it
 
Upvote 0
Top