Android Question Service and TTS - Solved

padvou

Active Member
Licensed User
Can a service module use TTS to speak?
If not how could this be accomplished?
 

padvou

Active Member
Licensed User
@lemonisdead

Here's the service module code:
B4X:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
    Dim TTS2 As TTS
Dim four As Long
End Sub
Sub Service_Create
Log("service created")
If TTS2.IsInitialized = False Then
        TTS2.Initialize("TTS2")
    End If
TTS2.SetLanguage("en","")
    DateTime.TimeFormat = "HH"
    Dim s As Int
    s = DateTime.Time(DateTime.now)
    ToastMessageShow(s,False)
    DateTime.TimeFormat = "mm"
    Dim s2 As Int
    s2 = DateTime.Time(DateTime.now)
Dim now As Long = DateTime.now
four = DateUtils.SetDateAndTime(DateTime.GetYear(now), DateTime.GetMonth(now), _
   DateTime.GetDayOfMonth(now),s, s2+2, 0)
If four < now Then
   'skip to tomorrow
   Dim p As Period
   p.Days = 1
   four = DateUtils.AddPeriod(four, p)
End If
Log(DateUtils.TicksToString(four))
End Sub

Sub Service_Start (StartingIntent As Intent)
Log("service started")
StartServiceAt("",four,True)
End Sub

Sub Service_Destroy
Log("service destroyed")
TTS2.Release
End Sub
Sub TTS2_Ready (Success As Boolean)
    If Success Then
    Log("TTS ready!")
        DateTime.TimeFormat = "HH:mm"
        Dim DateNow, TimeNow As String
DateNow= DateTime.Date(DateTime.Now)
TimeNow = DateTime.Time(DateTime.Now)
Log("Today is " & DateNow & " The time is  " & TimeNow)
    TTS2.Speak("Today is " & DateNow & " The time is  " & TimeNow, True)
    Log("spoken")
    Else
        Log("Error initializing TTS engine.")
    End If
End Sub
And the log lines:
B4X:
** Service (notifyuser) Create **
service created
04/13/2015 38
** Service (notifyuser) Start **
service started
TTS ready!
Today is 04/13/2015 The time is  20:36
spoken
But no speech!

and after some time, the log is full of these lines:
B4X:
** Service (notifyuser) Start **
service started
** Service (notifyuser) Start **
service started
** Service (notifyuser) Start **
service started
** Service (notifyuser) Start **
service started
** Service (notifyuser) Start **
 

lemonisdead

Well-Known Member
Licensed User
Here's the service module code
Quickly because I have to prepare diner for the children :
1/ have you checked you have the TTS data installed on the mobile
2/ have you checked the volume (I don't recall if it is the notification or the music channel)

Will come back to the thread a little later :)
 

padvou

Active Member
Licensed User
Quickly because I have to prepare diner for the children :
1/ have you checked you have the TTS data installed on the mobile
2/ have you checked the volume (I don't recall if it is the notification or the music channel)

Will come back to the thread a little later :)
Thank you for replying!
If I run the thing from an activity module, it works.
 

Attachments

lemonisdead

Well-Known Member
Licensed User
Something hurt me :
B4X:
TTS2.SetLanguage("en","")
I would put instead
B4X:
TTS2.SetLanguage("en","EN")
I will test your code later
 

lemonisdead

Well-Known Member
Licensed User
Attached is a way to test if the TTS works from a service on your phone.
When running, it will speak the time every five minutes. You can see that I used a separated service for the TTS engine as it takes some time to be ready and a planned service as the requester.
In the same time, I am keeping the PartialLock or the TTS engine's sound could not be heard on some devices when the screen is off.

On the layout you will see a START and a STOP buttons for the planner and a SPEAK button to test if you want.

Of course it is a rough sample only for demonstrate because in this example the TTS service will run forever. But the ttsservice could be started some minutes before the planner sends the sentence to be told and stopped some minutes after.

In my opinion, the problem you are facing is on the next execution as I've got an error in the logcat. Are you sure about that execution date ? I have to study its generation more closely.
 

Attachments

lemonisdead

Well-Known Member
Licensed User
Hum... That message came from line 52 of the "ttsservice" service (sub "Say" of the service).
This means that the TTS engine was successfully initialized but something else was in error.

Please can you try restarting the phone or check if the specified language is installed ?
 

Peter Simpson

Expert
Licensed User
I too use TTS in both my Star trek and Tix Clock widgets. There are no issues whatsoever with TTS in a Service.

I've just tested the code in the zip file @padvou and all appears to be working correctly. I can switch the language from en to de and I can hear a different language too. Are you sure that your phones TTS is working correctly as that zip file works perfect for me on my N5 and S4 phones...

Add the following to your long running service.
B4X:
#Region Service Attributes
#StartAtBoot: False
#StartCommandReturnValue: android.app.Service.START_STICKY
#End Region
You app also appears to be speaking by itself every few minutes. I've added START_STICKY to your service so now I'll leave it running all night just to see what happens...
 
Last edited:

padvou

Active Member
Licensed User
Hmmm,
the issue seems to be that the OS language wasn't English (US) which has full TTS support.
What would a workaround be?
 

padvou

Active Member
Licensed User
Hum... That message came from line 52 of the "ttsservice" service (sub "Say" of the service).
This means that the TTS engine was successfully initialized but something else was in error.

Please can you try restarting the phone or check if the specified language is installed ?
Strange thing is that, if I run the example from the forum on which the zip file I sent you is based, TTS speaks from the activity but remains silent in the service.
If I change the android language to English(US) service TTS speaks too...
Go figure..
 

padvou

Active Member
Licensed User
I too use TTS in both my Star trek and Tix Clock widgets. There are no issues whatsoever with TTS in a Service.

I've just tested the code in the zip file @padvou and all appears to be working correctly. I can switch the language from en to de and I can hear a different language too. Are you sure that your phones TTS is working correctly as that zip file works perfect for me on my N5 and S4 phones...

Add the following to your long running service.
B4X:
#Region Service Attributes
#StartAtBoot: False
#StartCommandReturnValue: android.app.Service.START_STICKY
#End Region
You app also appears to be speaking by itself every few minutes. I've added START_STICKY to your service so now I'll leave it running all night just to see what happens...
Thank you for your response.
That's the point of this app.
I wanted my smartphone to remind me every hour and talk to me. :)
 

lemonisdead

Well-Known Member
Licensed User
I'm sorry I have no more idea... That's what I hate with the difference of behavior in the Android's world : I never understand why something is working or not and not being able to be sure...
 

lemonisdead

Well-Known Member
Licensed User
I spoke too quickly, perhaps a workaround : why not starting a transparent activity when you want it to speak and close it just after ?
 
Top