I am trying to implement a TTS application for Android. Here is the code that I have written so far:
import android.app.Activity;
import android.content.Intent;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Locale;
public class AlarmActivity extends Activity implements OnClickListener, TextToSpeech.OnInitListener {
private TextToSpeech mTts;
private static final String TAG = "TextToSpeechDemo";
private static final int MY_DATA_CHECK_CODE = 1234;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.alarm);
Button btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(this);
btnAdd.setEnabled(false);
TextView txt = (TextView) findViewById(R.id.txt);
txt.setText("OnCreate");
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
TextView txt = (TextView) findViewById(R.id.txt);
if (requestCode == MY_DATA_CHECK_CODE)
{
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
{
txt.setText("Done result");
mTts = new TextToSpeech(this, this);
mTts.setLanguage(Locale.US);
Button btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setEnabled(true);
}
else
{
txt.setText("Missing");
Intent installIntent = new Intent();
installIntent.setAction(
TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
@Override
public void onDestroy()
{
if (mTts != null)
{
mTts.stop();
mTts.shutdown();
}
super.onDestroy();
}
@Override
public void onClick(View v) {
TextView txt = (TextView) findViewById(R.id.txt);
txt.setText("Click");
String myText1 = "Did you sleep well?";
String myText2 = "I hope so, because it time to wake up.";
mTts.speak(myText1, TextToSpeech.QUEUE_FLUSH, null);
mTts.speak(myText2, TextToSpeech.QUEUE_ADD, null);
}
@Override
public void onInit(int status) {
TextView txt = (TextView) findViewById(R.id.txt);
txt.setText("status 0");
if (status == TextToSpeech.SUCCESS) {
txt.setText("status 1");
int result = mTts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA ||
result == TextToSpeech.LANG_NOT_SUPPORTED) {
txt.setText("status 2");
} else {
Button btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setEnabled(true);
txt.setText("status 3");
}
} else {
txt.setText("status 4");
Log.e(TAG, "Could not initialize TextToSpeech.");
}
}
}
My layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alarms"
android:id="@+id/txt" />
<Button android:id="@+id/btnAdd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Say" />
</LinearLayout>
In the NetBeans ADB log, you can see the following:
13:08:41.314 163 INFO ActivityManager Starting activity: Intent { act=android.speech.tts.engine.CHECK_TTS_DATA cmp=com.svox.pico/.CheckVoiceData }
13:08:41.504 163 INFO ActivityManager Displayed activity org.me.talkingdroid/.MainActivity: 2790 ms (total 2790 ms)
13:08:41.544 163 WARN ActivityManager Binding with unknown activity: android.os.BinderProxy@46d91ac8
13:08:41.634 264 DEBUG [ScrollKPI] drawScreenCache takes 222ms, drawThumbnailCache takes 106ms
13:08:43.214 163 VERBOSE KeyInputQueue Enqueueing: MotionEvent{470684e0 action=0 x=310.26947 y=438.86896 pressure=0.15294118 size=0.13333334}
13:08:43.214 163 INFO WindowManager dispatchPointer MotionEvent{470684e0 action=0 x=310.26947 y=438.86896 pressure=0.15294118 size=0.13333334}
13:08:43.214 163 INFO WindowManager Delivering pointer QueuedEvent{470bdcb8 MotionEvent{470684e0 action=0 x=310.26947 y=438.86896 pressure=0.15294118 size=0.13333334}} to Window{46e0c640 org.me.talkingdroid/org.me.talkingdroid.MainActivity paused=false}
13:08:43.264 163 VERBOSE KeyInputQueue Enqueueing: MotionEvent{470684e0 action=1 x=310.26947 y=438.86896 pressure=0.15294118 size=0.13333334}
13:08:43.264 163 INFO WindowManager dispatchPointer MotionEvent{470684e0 action=1 x=310.26947 y=438.86896 pressure=0.15294118 size=0.13333334}
13:08:43.264 163 INFO WindowManager Delivering pointer QueuedEvent{46f34300 MotionEvent{470684e0 action=1 x=310.26947 y=438.86896 pressure=0.15294118 size=0.13333334}} to Window{46e0c640 org.me.talkingdroid/org.me.talkingdroid.MainActivity paused=false}
13:08:43.284 10637 INFO TTS received: Did you sleep well?
13:08:43.284 10637 INFO TTS received: I hope so, because it time to wake up.
13:08:46.714 10038 DEBUG dalvikvm GC_EXPLICIT freed 417 objects / 26192 bytes in 82ms
13:08:47.994 10637 DEBUG dalvikvm Debugger has detached; object registry had 398 entries
Obviously, the text that I am trying to output is sent from the journal, however I don’t hear anything from my phone. What could be the problem?
source
share