橘子味的心
标题:13.3 Android文本语音API教程

Android 7.0 提供了 TextToSpeech API,可以方便地实现将文本转化为语音的功能。借助于 TextToSpeech API,企业可以很容易地开发出自己的基于文本的语音播放应用程序。

TextToSpeechDemo 是一个使用 TextToSpeech API 进行文本语音播放的实例,修改自 Android SDK 自带的 Demo 实例,其运行效果如图 1 所示。点击 speak 按钮就可以将按钮下方显示的文本内容以语音方式播放出来。

文本语音播放
图 1  文本语音播放

其布局文件 text_to_speech.xml 的内容如下:
  1. <?xml version="l.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6.  
  7. <Button
  8. android:id="@+id/again_button"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:enabled="false"
  12. android:text="speak" />
  13.  
  14. <TextView
  15. android:id="@+id/textView1"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="Large Text"
  19. android:textAppearance="?android:attr/textAppearanceLarge" />
  20. </LinearLayout>
实例 TextToSpeechDemo 中 TextToSpeechActivity.java 的代码如下:
  1. package introduction.android.texttospeechdemo;
  2.  
  3. import java.util.Locale;
  4. import java.util.Random;
  5.  
  6. import android.app.Activity;
  7. import android.os.Bundle;
  8. import android.speech.tts.TextToSpeech;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.TextView;
  13.  
  14. public class TextToSpeechActivity extends Activity implements TextToSpeech.OnInitListener {
  15. private static final String TAG = "TextToSpeechDemo";
  16. private TextToSpeech mTts;
  17. private Button mAgainButton;
  18. private TextView tv;
  19.  
  20. @Override
  21. public void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_text_to_speech);
  24.  
  25. // Initialize text-to-speech. This is an asynchronous operation.
  26. // The OnInitListener (second argument) is called after initialization completes.
  27. mTts = new TextToSpeech(this, this// TextToSpeech.OnInitListener
  28. );
  29. tv = (TextView) this.findViewById(R.id.textView1);
  30.  
  31. // The button is disabled in the layout.
  32. // It will be enabled upon initialization of the TTS engine.
  33. mAgainButton = (Button) findViewById(R.id.again_button);
  34.  
  35. mAgainButton.setOnClickListener(new View.OnClickListener() {
  36. public void onClick(View v) {
  37. sayHello();
  38. }
  39. });
  40. }
  41.  
  42. @Override
  43. public void onDestroy() {
  44. // Don't forget to shutdown!
  45. if (mTts != null) {
  46. mTts.stop();
  47. mTts.shutdown();
  48. }
  49. super.onDestroy();
  50. }
  51.  
  52. // Implements TextToSpeech.OnInitListener.
  53. public void onInit(int status) {
  54. // status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
  55. if (status == TextToSpeech.SUCCESS) {
  56. // Set preferred language to US english.
  57. // Note that a language may not be available, and the result will indicate this,
  58. int result = mTts.setLanguage(Locale.US);
  59. // Try this someday for some interesting results.
  60. // int result mTts.setLanguage(Locale.FRANCE);
  61. if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
  62. // Lanuage data is missing or the language is not supported.
  63. Log.e(TAG, "Language is not available.");
  64. } else {
  65. // Check the documentation for other possible result codes.
  66. // For example, the language may be available for the locale,
  67. // but not for the specified country and variant.
  68.  
  69. // The TTS engine has been successfully initialized.
  70. // Allow the user to press the button for the app to speak again.
  71. mAgainButton.setEnabled(true);
  72. // Greet the user.
  73. sayHello();
  74. }
  75. } else {
  76. // Initialization failed.
  77. Log.e(TAG, "Could not initialize TextToSpeech.");
  78. }
  79. }
  80.  
  81. private static final Random RANDOM = new Random();
  82. private static final String[] HELLOS = {
  83. "Hello",
  84. "Congratulation",
  85. "Greetings",
  86. "How are you!",
  87. "What's your name?",
  88. "I am a good student!",
  89. "Oh My God!"
  90. };
  91.  
  92. private void sayHello() {
  93. // Select a random hello.
  94. int helloLength = HELLOS.length;
  95. String hello = HELLOS[RANDOM.nextInt(helloLength)];
  96. mTts.speak(hello, TextToSpeech.QUEUE_FLUSH, // Drop all pending entries in the playback queue,
  97. null);
  98. tv.setText(hello);
  99. }
  100. }
在这段内码中:
  1. private static final String[] HELLOS = {
  2. "Hello",
  3. "Congratulation",
  4. "Greetings",
  5. "How are you!",
  6. "What's your name?",
  7. "I am a good student!",
  8. "Oh My God!"
  9. };
HELLOS 数组中存放了用于播放的文本,读者如果想播放自己的文本,只需要替换该数组内的字符串即可。

mTts = new TextToSpeech(this,this);

创建了 TextToSpeech 实例 mTts,该构造方法的第一个参数表示容纳该对象的容器,第二个参数表示实现文本语音回调接口 TextToSpeech.OnInitListener 的类,该接口提供一个 public void onInit(int status) 方法,用于对文本语音 API 进行初始化。

TextToSpeech 实例通过 speak 方法即可进行语音播放。本实例是以随机顺序播放 HELLOS 中的字符串的。

目前,该功能对英文支持较好,暂不支持中文文本播放。