Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use a static TextToSpeech in Android?

The TextToSpeech constructor looks like it's designed to be 'owned' by an Activity. I'm producing an app with multiple different Activities, and I don't want to have to initialise a new TextToSpeech instance for each - I want the speech to carry on smoothly even if the Activity is changing.

My idea is to have a static TextToSpeech object accessed by all activities, initialised by the first one.

  1. Does anyone know if the TextToSpeech implementation is thread-safe? I'm guessing not, but someone out there might know.
  2. If I initialise it with the Context of my default Activity, will the TextToSpeech instance stop working when the Activity is destroyed?
like image 668
hcarver Avatar asked Jul 06 '12 14:07

hcarver


People also ask

How do you pause text-to-speech on Android?

There isn't a pause event or method, so you can not pause and resume. Unfortunately, the way the Android TTS interface is designed makes it next to impossible to implement a pause/resume feature, at least not without a lot of extra bookkeeping. The two main APIs it provides are a speak() call and a stop() call.

What is AddSpeech () method in Android?

AddSpeech(String, String, Int32) Adds a mapping between a string of text and a sound resource in a package. AddSpeech(ICharSequence, String, Int32) Adds a mapping between a CharSequence (may be spanned with TtsSpans) of text and a sound resource in a package.

How to use TextToSpeech class in Android?

The below example demonstrates the use of TextToSpeech class. It crates a basic application that allows you to set write text and speak it. To experiment with this example , you need to run this on an actual device. You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication.

How to convert text to speech using TextToSpeech?

In android, you can convert your text into speech by the help of TextToSpeech class. After completion of the conversion, you can playback or create the sound file. The commonly used methods of TextToSpeech class are as follows: converts the text into speech.

What is text to speech in Android?

Android - Text To Speech. Android allows you convert your text into voice. Not only you can convert it but it also allows you to speak text in variety of different languages. Android provides TextToSpeech class for this purpose.

How do I enable text-to-speech (TTS) on my Device?

Scroll down and tap Accessibility . It's near the bottom of the page, next to the stick-figure icon. Tap Text-to-speech output. It's above the "Display" section on the page. Select a TTS engine. If your phone manufacturer provides their own Text-to-speech engine, you'll see more than one option available.


3 Answers

I have never tried that, but I think you can pass an Application context as the parameter in the constructor, not necessarily an Activity.

But paying attention to the documentation, I see that the TTS engine has its own queing system, so you can call speak several times without worrying about the thread timing.

Regardind to your questions, I'm not sure about the number two, but as I wrote first, I would try passing an Application context, rather than Activity context.

About number one, well, there is one instance per engine at a time, I guess. And you normally have just one engine, but again, if the engine controls queries queuing, don't worry about the threads.

like image 133
mdelolmo Avatar answered Oct 12 '22 19:10

mdelolmo


Thanks to those that told me to pass the ApplicationContext. Turned out that was the easy bit... The hard bit was whether the TextToSpeech object is guaranteed thread-safe.

Thanks for answers telling me how to make something thread-safe / assuming that it is, but the question was about whether the object already is. I probably should have said, I'm fine with implementing thread-safety, but I wanted to know whether I need to bother. And I don't want to assume thread-safety without being certain.

I ran the following and it seemed to work. So I assume the Android SDK TTS is thread-safe, but can't find any documentation saying that it's safe to assume this across all devices, so I'll be wrapping my TTS instance for the time being!

package com.example.testproject;

import java.util.Random;

import android.os.Bundle;
import android.app.Activity;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;

public class TestActivity extends Activity implements OnInitListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tts = new TextToSpeech(getApplicationContext(), this);
    }

    TextToSpeech tts = null;

    @Override
    public void onInit(int arg0) {
        for (int i = 0; i < 100; ++i) {
            class Irritate implements Runnable {
                Irritate(int iIn) {
                    i = iIn;
                }

                @Override
                public void run() {
                    Random r = new Random();
                    try {
                        Thread.sleep(r.nextInt(2000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    tts.speak(Integer.toString(i), TextToSpeech.QUEUE_ADD, null);
                }

                int i;
            }

            Thread t = new Thread(new Irritate(i));

            t.start();
        }
    }
}
like image 25
hcarver Avatar answered Oct 12 '22 18:10

hcarver


I've always used TTS as an Activity that I startedForResult. I just fire an intent to it and then wait for it to come back. If I remember correctly, if returns an array of answers sorted by confidence. So you if you don't have a Context, then I don't believe there is another way to call it (at least using this model). Not sure if there is an object reference that you can get for it.

However, if there is, to use your idea. Then you can just extend Application and hold the static reference to your TTS in there. That way it's visible to all your Activities. I think this is answer you are looking for.

like image 1
Frank Sposaro Avatar answered Oct 12 '22 20:10

Frank Sposaro