0% found this document useful (0 votes)
30 views

Text-To-Speech Code Sample

The document is the code for an Android application that uses text-to-speech to randomly select phrases from an array and speak them aloud. It initializes text-to-speech, sets language and playback settings, and contains methods to handle button clicks to either play a random phrase or launch the next activity.

Uploaded by

Meghan Pivane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Text-To-Speech Code Sample

The document is the code for an Android application that uses text-to-speech to randomly select phrases from an array and speak them aloud. It initializes text-to-speech, sets language and playback settings, and contains methods to handle button clicks to either play a random phrase or launch the next activity.

Uploaded by

Meghan Pivane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

package com.androidproj.

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

public void onPause() {


if (tts != null) {
tts.stop();
// tts.shutdown();
}
super.onPause();
}
}

You might also like