Android Question Service and TTS - Solved

Discussion in 'Android Questions' started by padvou, Apr 13, 2015.

  1. padvou

    padvou Active Member Licensed User

    Can a service module use TTS to speak?
    If not how could this be accomplished?
     
  2. DonManfred

    DonManfred Expert Licensed User

    I think the answer is NO on both questions.

    TTS needs an Activity to record the sound.

    But i´m not sure about the answer. Maybe i´m wrong
     
  3. lemonisdead

    lemonisdead Well-Known Member Licensed User

    Hello @padvou

    I use TTS from services with no problem. You should make a try :)
     
    DonManfred likes this.
  4. DonManfred

    DonManfred Expert Licensed User

    Ok, then i´m wrong ;)
    I thought it needs an activity-object
     
    lemonisdead likes this.
  5. padvou

    padvou Active Member Licensed User

    @lemonisdead

    Here's the service module code:
    Code:
    #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+20)
    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:
    Code:
    ** 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:
    Code:
    ** Service (notifyuser) Start **
    service started
    ** 
    Service (notifyuser) Start **
    service started
    ** 
    Service (notifyuser) Start **
    service started
    ** 
    Service (notifyuser) Start **
    service started
    ** 
    Service (notifyuser) Start **
     
  6. lemonisdead

    lemonisdead Well-Known 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 :)
     
    padvou likes this.
  7. padvou

    padvou Active Member Licensed User

    Thank you for replying!
    If I run the thing from an activity module, it works.
     

    Attached Files:

    • tts.zip
      File size:
      8.7 KB
      Views:
      31
  8. lemonisdead

    lemonisdead Well-Known Member Licensed User

    Something hurt me :
    Code:
    TTS2.SetLanguage("en","")
    I would put instead
    Code:
    TTS2.SetLanguage("en","EN")
    I will test your code later
     
    Beja and padvou like this.
  9. padvou

    padvou Active Member Licensed User

    Unfortunately, no joy..
     
  10. lemonisdead

    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.
     

    Attached Files:

    Beja likes this.
  11. padvou

    padvou Active Member Licensed User

    I get "Error in ttsservice" in the log
     
  12. lemonisdead

    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 ?
     
  13. Peter Simpson

    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.
    Code:
    #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: Apr 14, 2015
    lemonisdead likes this.
  14. padvou

    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?
     
  15. padvou

    padvou Active Member Licensed User

    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..
     
  16. padvou

    padvou Active Member Licensed User

    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. :)
     
  17. Peter Simpson

    Peter Simpson Expert Licensed User

    Hello,
    Well your app ran all night without any issues and it's still chatting to me :D
     
    padvou likes this.
  18. lemonisdead

    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...
     
    padvou likes this.
  19. padvou

    padvou Active Member Licensed User

    Yeap! I will try it on several other devices and see what happens. Damn these android incompatibilities...
     
    lemonisdead likes this.
  20. lemonisdead

    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 ?
     
    Beja and padvou like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice