Thanks to those who told me to pass ApplicationContext. It turned out that it was an easy bit ... The hard bit was whether the TextToSpeech object was guaranteed to be thread safe.
Thanks for the answers telling me how to make something thread safe / assuming this is the case, but the question was whether the object is already there. I probably should have said that I was fine with implementing security flows, but I wanted to know if I needed to worry. And I don't want to assume thread safety without being sure.
I ran the following and it seemed to work. Therefore, I believe that the Android TTS SDK is thread safe, but cannot find any documentation that says it is safe to read this on all devices, so I will wrap my copy of TTS for now!
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(); } } }
source share