Android Question TTS Stop

Discussion in 'Android Questions' started by AlpVir, Jul 15, 2015.

  1. AlpVir

    AlpVir Well-Known Member Licensed User

    It is possible to stop (with a KEYCODE_BACK) the pronunciation of a long text ?
     
  2. lemonisdead

    lemonisdead Well-Known Member Licensed User

    Hello,
    It exists TTS.Stop and TTS.Release. don't they work for you ?
     
    AlpVir likes this.
  3. AlpVir

    AlpVir Well-Known Member Licensed User

    Thanks. I had missed the existence of these instructions !
     
  4. AlpVir

    AlpVir Well-Known Member Licensed User

    Actually my code does not allow to stop the delivery of a long speech as desired.
    The speech is divided into several parts and between a part and the other there is an interval of time. See my post :
    http://www.b4x.com/android/forum/threads/pause-in-tts-engine.55867/#post-351752
    Unfortunately it happens sometimes that shaking the smartphone app abruptly terms. It 'a very mysterious fact that it's hard to investigate.
    If you comment the DoEvents statement in the function Sleep app this interruption does not occur, but the same can not stop (with a KEYCODE_BACK) the pronunciation of the text.
    I apologize for my bad English. I do not know if I managed to make myself understood.

    Code:
    '  Msg="first word /P1000 second word /P500 pippo"
        HesTalking=True
        
    Try
            ele = 
    Regex.Split(" /P",Msg)
            
    For i=0 To ele.Length-1
                Posiz=ele(i).IndexOf2 (
    " ",0)
                
    If Posiz>-1 Then
                    S=ele(i).SubString2 (
    0,Posiz)
                    
    If S<>"" Then
                        
    If IsNumber(S) Then
                            ele(i)=ele(i).SubString(Posiz+
    1)
                            Sleep (S)
                            
    If HesTalking=True Then TTS1.Speak(ele(i), False)
                        
    Else
                            
    If HesTalking=True Then TTS1.Speak(ele(i), False)
                        
    End If   
                    
    End If  
                
    Else
                    
    If HesTalking=True Then TTS1.Speak(ele(i), False)      
                
    End If
            
    Next
        
    Catch
            
    Log ("Error X")
        
    End Try
        HesTalking=
    False
    Code:
    Sub Sleep(ms As Long)
        
    Dim now As Long
        now=
    DateTime.Now
        
    Do Until (DateTime.Now>now+ms)
             
    DoEvents   ' This causes an exit from the app !!!!!!!!!!!!
        Loop
    End Sub
    Code:
    Sub Activity_KeyPress (KeyCode As Int) As Boolean
        
    If KeyCode = KeyCodes.KEYCODE_BACK Then  
            
    If HesTalking=True Then
                TTS1.Stop
                HesTalking=
    False
            
    End If
            
    Return True
        
    End If       
    End Sub
     
    lemonisdead likes this.
  5. lemonisdead

    lemonisdead Well-Known Member Licensed User

    Hello,
    The exit from the app (the DoEvents issue) could be avoided by putting all the necessary code related to TTS in a service and not the activity, I think.

    I have tried to reproduce the "not stop" issue by myself and saw that sometimes the HesTalking was False even if the TTS was still speaking.
    Erel provided a code to detect when the TTS has stopped :http://www.b4x.com/android/forum/threads/tts.7500/#post-207738
    Perhaps it could help ?
     
  6. AlpVir

    AlpVir Well-Known Member Licensed User

    The attached sample works and stops the delivery of the sentence. But I have added a button with the function of STOP.
    Now I have to understand why this same code (more or less) does not work in my app.
    Code:
    Sub Process_Globals
    End Sub

    Sub Globals
        
    Dim ButtonSpeak            As Button
        
    Dim ButtonStop            As Button
        
    Dim TTS1                As TTS
        
    Dim HesTalking            As Boolean 
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        ButtonSpeak.Initialize (
    "ButtonSpeak")
        
    Activity.AddView (ButtonSpeak,10dip,10dip,100dip,50dip)
        ButtonSpeak.Text = 
    "SPEAK"
       
        ButtonStop.Initialize (
    "ButtonStop")
        
    Activity.AddView (ButtonStop,10dip,70dip,100dip,50dip)
        ButtonStop.Text = 
    "STOP"
        
    If TTS1.IsInitialized = False Then
            TTS1.Initialize(
    "TTS1")
        
    End If
    End Sub

    Sub Activity_Resume
    End Sub

    Sub Activity_Pause (UserClosed As Boolean)
    End Sub

    Sub ButtonSpeak_click
        
    Dim Msg         As String
       
        Msg=
    "Hello "
        Msg = Msg & 
    " /P2000 Rapid Application Development ( /P500 rad) tools for native Android, iOS and desktop apps."
        Msg = Msg &
    " /P4000 The simplest way To develop powerful, cross platform solutions."
        Speak (Msg)
    End Sub

    Sub ButtonStop_click
        HesTalking=
    False
        TTS1.Stop
        
    Log ("STOP")
    End Sub

    Sub Speak (Msg As String)
         
    Dim ele()                     As String
        
    Dim i                        As Int
        
    Dim S                        As String
        
    Dim Posiz                    As Int    
        
    Log ("SPEAK")
        HesTalking=
    True

        ele = 
    Regex.Split(" /P",Msg)
        
    For i=0 To ele.Length-1
            Posiz=ele(i).IndexOf2 (
    " ",0)
            
    If Posiz>-1 Then
                S=ele(i).SubString2 (
    0,Posiz)
                
    If S<>"" Then
                    
    If IsNumber(S) Then
                        ele(i)=ele(i).SubString(Posiz+
    1)
                        
    Log ("sleep " & S)
                        Sleep (S)
                        
    Log (ele(i))
                        
    If HesTalking=True Then TTS1.Speak(ele(i), False)
                    
    Else
                        
    Log (ele(i))   
                        
    If HesTalking=True Then    TTS1.Speak(ele(i), False)
                    
    End If    
                
    End If   
            
    Else
                
    Log (ele(i))
                
    If HesTalking=True Then TTS1.Speak(ele(i), False)       
            
    End If
        
    Next

    End Sub

    Sub Sleep(ms As Long)
        
    Dim now As Long
        now=
    DateTime.Now
        
    Do Until (DateTime.Now>now+ms)
             
    DoEvents 
        
    Loop
    End Sub
     

    Attached Files:

  7. Erel

    Erel Administrator Staff Member Licensed User

    You should never block the main thread with "do events loop". It will only cause problems. Use a timer instead.
     
Loading...