Text-To-Speech Code Sample
Text-To-Speech Code Sample
learningabc;
import
import
import
import
import
import
import
import
android.app.Activity;
android.content.Intent;
android.os.Bundle;
android.speech.tts.TextToSpeech;
android.view.View;
android.view.View.OnClickListener;
android.widget.Button;
android.widget.ImageButton;
import java.util.Locale;
import java.util.Random;
public class OpenActivity extends Activity implements OnClickListener {
static final String[] texts = {"Welcome student", "Hi Renalyn", "Lets learn
ABC", "Hi I am your teacher", "Whats up Edward", "Lets start", "Good evening Sir
Adducul", "Have fun learning", "Yo Cristian", "Hello John Paul", "Sing a song",
"English Please Red", "Nice try JH"};
TextToSpeech tts;
@Override
protected void onCreate(Bundle startApp) {
// TODO Auto-generated method stub
super.onCreate(startApp);
setContentView(R.layout.open);
ImageButton ms = (ImageButton) findViewById(R.id.manSpeak);
Button start = (Button) findViewById(R.id.next);
ms.setOnClickListener(this);
start.setOnClickListener(this);
tts = new TextToSpeech(OpenActivity.this, new TextToSpeech.OnInitListene
r() {
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
tts.setLanguage(Locale.US);
tts.setPitch((float) 0.70);
tts.setSpeechRate(1);
}
}
});
}
public void onClick(View start) {
if (start.getId() == R.id.manSpeak) {
Random r = new Random();
String random = texts[r.nextInt(13)];
tts.speak(random, TextToSpeech.QUEUE_FLUSH, null);
} else if (start.getId() == R.id.next) {
Intent k = new Intent(OpenActivity.this, MainActivity.class);
startActivity(k);
}
}
@Override