将文本转换成语音输出
package com.example.androidapi.app;
import java.util.Locale;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.example.androidapi.R;
public class TextToSpeechActivity extends Activity implements TextToSpeech.OnInitListener {
private static final String TAG = "TextToSpeechDemo";
private TextToSpeech mTts;
private Button mAgainButton;
private static final Random RANDOM = new Random();
private static final String[] HELLOS = {
"Hello","Salutations","Greetings",
"Howdy","What's crack-a-lackin?","That explains the stench!"
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_to_speech);
mTts = new TextToSpeech(this,this );
mAgainButton = (Button) findViewById(R.id.again_button);
mAgainButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sayHello();
}
});
}
private void sayHello() {
int helloLength = HELLOS.length;
String hello = HELLOS[RANDOM.nextInt(helloLength)];
mTts.speak(hello, TextToSpeech.QUEUE_FLUSH, null);
}
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = mTts.setLanguage(Locale.UK);
Log.e("dddd", "result==="+result);
if (result == TextToSpeech.LANG_MISSING_DATA ||
result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e(TAG, "Language is not available.");
} else {
mAgainButton.setEnabled(true);
sayHello();
}
} else {
Log.e(TAG, "Could not initialize TextToSpeech.");
}
}
@Override
public void onDestroy() {
if (mTts != null) {
mTts.stop();
mTts.shutdown();
}
super.onDestroy();
}
}