Android Question Text to Speech stay initialized in service?

Michael Gasperi

Member
Licensed User
Longtime User
I wanted to monitor the battery level through out the day by simply having a service announce verbally the level as it changed. Does the Text to Speech engine stay initialized all by itself or should I check its state every time I want it to speak?

Code for Service follows:
B4X:
Sub Process_Globals
    Dim PE As PhoneEvents
    Dim TTS1 As TTS
    Dim OldValue As Int
End Sub
Sub Service_Create
    TTS1.Initialize("TTS1")
End Sub
Sub PE_BatteryChanged (Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent)
    If OldValue <> Level Then
        TTS1.Speak(Level,True)
    End If
    OldValue = Level
End Sub
Sub TTS1_Ready (Success As Boolean)
    PE.Initialize("PE")
End Sub
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
Hello,
In my opinion, till you don't TTS1.Release, it should stay initialized
 
Upvote 0

Michael Gasperi

Member
Licensed User
Longtime User
That is how it seems to be working too. I've tried using the TTS while this service is running, and it doesn't seem to be tied up or anything.

Would you think it would be better to initialize and then release each time you want to say something? In other words, does it cost memory or battery to keep it initialized all the time like this?
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Yes, it uses memory, Michael. If you take a look at your running applications, you will see that the TTS is tied to your running service.
If you choose to release the TTS when not needed, you'll be facing this problem : on some devices, you won't get the TTS finish information correctly so you could release it before it has finished to speak. But there is a code that Erel posted (somewhere on the forum) which will allow you to test if the TTS has really finished speaking.
 
Upvote 0

Michael Gasperi

Member
Licensed User
Longtime User
I can see that the service is a whole lot bigger than the tiny bit of code I wrote. Given the risk and complexity of releasing and initializing it is probably better to just leave it alone. At least monetarily, it would need that much memory to speak anyway.

The good news is that it does seem to stay initialized which is what I was worried about. Thanks
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…