Reputation: 41
I am trying to write a super simple app that does TextToSpeech, when a button is clicked.
I ran the app, and on the first time, the TTS worked! (i heard it speak). When I ran it again (and many more time), it didn't speak, even though the code stayed the same. Could anyone tell me how to work it out?
I guess I'm missing something fundamental about how the TTS works, so I'd really appreciate it if you could explain it for a dummy.
Thank you very much, Julius
Here's the layout file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="test1"
android:text="Test" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open local DB" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open global DB" />
</LinearLayout>
The MainActivity code:
public class MainActivity extends Activity {//0
public void test1(View myView) {
int e = 1;
System.out.println("Starting test.....");
//Step s1=new Step();
Toast.makeText(getApplicationContext(), "aaaaaaa", Toast.LENGTH_SHORT).show();
Speaker.speak("aaaaaaaaaa", TextToSpeech.QUEUE_FLUSH, null);
Speaker.stop();
Speaker.shutdown();
}
TextToSpeech Speaker;
@Override
protected void onCreate(Bundle savedInstanceState) {//1
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Speaker = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {//2
@Override
public void onInit(int status) {//3
if (status == TextToSpeech.SUCCESS) System.out.println("success!!!");
if (status != TextToSpeech.ERROR) {//4
Speaker.setLanguage(Locale.UK);
}//4
else {
System.out.println("speak err");
}
}//3
}//2
);
}//1
@Override
public void onPause() {
if (Speaker != null) {
Speaker.stop();
Speaker.shutdown();
}
super.onPause();
}//1*/
}//0
And Finally (thanks for reading until now...) Here's the LogCat:
05-11 14:57:35.149 30221 30221 I art com.mycompany.myapp Late-enabling -Xcheck:jni
05-11 14:57:35.460 30221 30221 I TextToSpeech com.mycompany.myapp Sucessfully bound to com.google.android.tts
05-11 14:57:35.470 30221 30246 D OpenGLRenderer com.mycompany.myapp Use EGL_SWAP_BEHAVIOR_PRESERVED: true
05-11 14:57:35.535 30221 30221 D Atlas com.mycompany.myapp Validating map...
05-11 14:57:35.639 30221 30246 I Adreno-EGL com.mycompany.myapp <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 01/14/15, ab0075f, Id3510ff6dc
05-11 14:57:35.641 30221 30246 I OpenGLRenderer com.mycompany.myapp Initialized EGL, version 1.4
05-11 14:57:35.666 30221 30246 D OpenGLRenderer com.mycompany.myapp Enabling debug mode 0
05-11 14:57:35.907 30221 30221 I TextToSpeech com.mycompany.myapp Connected to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
05-11 14:57:35.918 30221 30268 I TextToSpeech com.mycompany.myapp Set up connection to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
05-11 14:57:35.919 30221 30221 I System.out com.mycompany.myapp success!!!
05-11 14:57:36.917 30221 30221 I System.out com.mycompany.myapp Starting test.....
05-11 14:57:36.985 30221 30246 V RenderScript com.mycompany.myapp Application requested CPU execution
05-11 14:57:37.005 30221 30246 V RenderScript com.mycompany.myapp 0xb4819e00 Launching thread(s), CPUs 4
05-11 14:57:38.465 30221 30221 W TextToSpeech com.mycompany.myapp stop failed: not bound to TTS engine
05-11 14:57:38.465 30221 30221 W TextToSpeech com.mycompany.myapp shutdown failed: not bound to TTS engine
Thanks Again
Upvotes: 1
Views: 408
Reputation: 32780
public void shutdown ()
Releases the resources used by the TextToSpeech engine. It is good practice for instance to call this method in the onDestroy() method of an Activity so the TextToSpeech engine can be cleanly stopped.Reference: http://developer.android.com/reference/android/speech/tts/TextToSpeech.html#shutdown()
For this reason the TTS works only the first time, you release the resources of TextToSpeech.
You should remove Speaker.shutdown();
from test1
and put in onDestroy
:
@Override
protected void onDestroy() {
Speaker.shutdown();
super.onDestroy();
}
Upvotes: 1