TTS in Service

schimanski

Well-Known Member
Licensed User
Longtime User
Hello!

I start and stop the following service with Startservice(TTSService) and Stopservice(TTSService) in the main sub, when a checkbox is true. But after stopping, the service starts always again:confused:

B4X:
'Service module
Sub Process_Globals
  Dim TTS1 As TTS
  Dim TTSReady as boolean
End Sub

Sub Service_Create
  TTSReady=false
  If TTS1.IsInitialized = False Then
    TTS1.Initialize("TTS1")
  End If
  TTS_Settings
End Sub

Sub Service_Start
  StartServiceAt("", DateTime.Now + 7*DateTime.TicksPerSecond, True)
  if TTSReady then TTS1.Speak("This is my Text",True)
End Sub

Sub Service_Destroy
  TTS1.Release
End Sub

Sub TTS_Settings
  TTS1.SpeechRate = 1.5
  TTS1.Pitch = 1.5
  TTS1.SetLanguage("de","")
End Sub

Sub TTS1_Ready (Success As Boolean)   
  If Success Then TTSReady=True
End Sub

Then i have tried it with a timer in the service. That runs for a few minutes, but then the service stops with an error. Should the timer be the problem?

B4X:
Sub Service_Create
  TimerTTS.Initialize("TimerTTS",7000)
  If TTS1.IsInitialized = False Then
    TTS1.Initialize("TTS1")
  End If
  TTS_Settings
End Sub

Sub TTS1_Ready (Success As Boolean)   
  If Success Then TimerTTS.Enabled=True
End Sub

Sub TimerTTS_Tick
  TTS1.Speak("This is my Text",True)
end sub

Thanks for help....
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
StopService doesn't cancel scheduled tasks. You can use a process global boolean variable (ShouldStop) that if true you immediately return from Service_Start.

However it is better to use a time if you want to run it every 7 seconds.
What error do you get in the Logs?
Make sure not to destroy the service in the second implementation.
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Thanks, Erel!

It was the first time, i used the logcat :sign0161: and found a numberformatexception in the service. It works....

But only one word to timers in services: I thought, they are problematically or have I missunderstood it?

Rgds
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
I am queueing several mp3 files (very short files) to play in a service. If the mp3 file does not exist, then I have the service "speak" the alternate text within the service.
In my main app, I have an array of file names that as I need the "noise", I add the filename of the mp3 file and alternate text to the array. I then startservice(TTSservice).
TTSservice plays the first file in the queue and if there are more, then it plays them until the queue is empty.
Within my main program I call a sub that adds the file to the queue and then does the startservice.
It all works very well, but when I debug my program (both under 1.8 and 1.9 version of b4a), i can step through to the call to the sub (where the service is started) and then the debugger either never returns or returns past many breakpoints (doesn't stop at the next break).
Any ideas? BTW, I call startservice for every time i add a file to the queue.
Thanks

Main program:
B4X:
MediaQueue(MediaPointer).Vox = Vox
      MediaQueue(MediaPointer).VoxFldr = VoxFldr
      MediaQueue(MediaPointer).Alternate = Alternate
      MediaQueue(MediaPointer).Wait = DoWait
      MediaPointer = MediaPointer + 1
      StartService(TTSService)
Service:
B4X:
Sub Service_Create
   TTSReady=False
   If TTS1.IsInitialized = False Then
      TTS1.Initialize("TTS1")
      MediaPlayer1.Initialize2("MediaPlayer1")
   End If
   TTS_Settings
   Main.MediaServiceIsRunning = True
End Sub

Sub Service_Start
   If Main.MediaPointer > 0 Then
      If Main.MediaQueue(0).Vox.trim <> "" Then          'there is a vox file to play   
         MediaPlayer1_Complete                     'cause it to play
      Else
         If Main.MediaQueue(0).Alternate.Trim <> "" Then                  'there is alternate text to speak
            If TTSReady Then 
               TTS1.Speak(Main.MediaQueue(0).Alternate, True)   'so speak it
            End If
         End If
      End If
   Else                              'they've cleared the queue and want it to stop talking
      TTS1.Stop
      MediaPlayer1.Stop
   End If
End Sub

Sub Service_Destroy
   MediaPlayer1.Stop
   MediaPlayer1.Release
     TTS1.Release
End Sub

Sub TTS_Settings
   TTS1.SpeechRate = 1
   TTS1.Pitch = 1
   TTS1.SetLanguage("en","US")
End Sub

Sub TTS1_Ready (Success As Boolean)    
   If Success Then TTSReady=True
End Sub

'When the media is finished see if there is more to play
Sub MediaPlayer1_Complete
   If Main.MediaQueue(0).vox <> "" Then
      MediaPlayer1.Load(Main.MediaQueue(0).VoxFldr, Main.MediaQueue(0).Vox)
      MediaPlayer1.Play
      RemoveFromMediaQueue(0)      'remove it from the queue
   End If
End Sub
 
Upvote 0
Top