turlando
turlando

Reputation: 174

Unable to instanciate TextToSpeech only on speak() request

I'm trying to implement TextToSpeech in a service that could be running for long times, so I'd like to instantiate TTS only if very necessary, for memory/battery saving reasons.

Is there a way to implement this handling multiple speak() requests?

In example, the application needs to use TTS: TTS is instantiated and initialised, once ready then speak is processed and finally shutdown(). But if TTS is already speaking (or instantiated) and the application needs another text synthesis, I would like that this call could be handled through the still alive TTS instance rather than a new one (in order to obtain flush ability).

Is there a clean and functional way to implement this?

Thank you very much in advance.

ADDENDUM: Do you think that this solution of creating and destroying will really help with battery saving or should I leave TTS in memory and let Android system manage everything?

Upvotes: 0

Views: 100

Answers (1)

Falco
Falco

Reputation: 3416

I would implement this using a Singleton/Queue Combination.

If a Text-To-Speak Command is issued you Enqueue the request (Simply using a ConcurrentQueue in Java and some custom Request Object from a self designed class) Then if the second Thread is not running it is started, otherwise it is notified.

The second Thread instanciates TTS, polls the first item in the Queue, and processes it. Then it will check the Queue again, if the Queue is empty it will shutdown TTS and exit, else it will process the next element in the Queue.

You can optionally add a timeout, so the Thread will wait for X Seconds after the last TTS Request, before shutting down.

Upvotes: 1

Related Questions