package com.anhuioss.tts;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class TTSActivity extends Activity implements OnClickListener, OnInitListener {
Button speechButton;
TextView speechText;
TextToSpeech textToSpeech;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
speechButton = (Button) findViewById(R.id.button1);
speechText = (TextView) findViewById(R.id.textView1);
speechButton.setOnClickListener(this);
textToSpeech = new TextToSpeech(this, this);
}
@Override
public void onClick(View v) {
if (textToSpeech != null && !textToSpeech.isSpeaking()) {
textToSpeech.speak(speechText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(this, "Denotes the language data is missing or the language is not supported.", Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onStop() {
super.onStop();
textToSpeech.stop();
textToSpeech.shutdown();
}
}