Android中TextToSpeech的使用

1,226 阅读3分钟

这是我参与8月更文挑战的第26天,活动详情查看:8月更文挑战

系列文章目录

Android中TextToSpeech的使用


前言

在一年前,和朋友一起码了一个英语APP,仿照某APP实现了单词的功能,最开始就是借助的TextToSpeech,后面感觉声音不够好听,于是选择使用第三方。

APP初稿如图:

7321571df291c894a5c85bda17d9ccc_副本.jpg


实现

1.初始化语音。这是一个异步操作。初始化完成后调用oninitListener(第二个参数)。

TextToSpeech mTts = new TextToSpeech(this, this);

2.实现TextToSpeech.OnInitListener

注意:语言可能不可用。

 // 实现TextToSpeech.OnInitListener.
     public void onInit(int status) {
         if (status == TextToSpeech.SUCCESS) {
             //设置首选语言为中文,注意,语言可能是不可用的,结果将指示此
             int result = mTts.setLanguage(Locale.CHINA);
             if (result == TextToSpeech.LANG_MISSING_DATA ||
                 result == TextToSpeech.LANG_NOT_SUPPORTED) {
                 //语言数据丢失或不支持该语言。
                 Log.e(TAG, "语言数据丢失或不支持该语言");
             } else {
                 //检查文档中其他可能的结果代码。
                 // 例如,语言可能对区域设置可用,但对指定的国家和变体不可用
                 // TTS引擎已成功初始化。
                 // 允许用户按下按钮让应用程序再次发言。
                 mAgainButton.setEnabled(true);
             }
         } else {
             // 初始化失败
             Log.e(TAG, "初始化失败");
         }
     }

3.写一个朗读方法,在需要的时候触发(如:点击事件)

TextToSpeech的speak方法有两个重载。

  • 执行朗读的方法
speak(CharSequence text,int queueMode,Bundle params,String utteranceId);

第二个参数queueMode用于指定发音队列模式,两种模式选择。 (1)TextToSpeech.QUEUE_FLUSH:该模式下在有新任务时候会清除当前语音任务,执行新的语音任务 (2)TextToSpeech.QUEUE_ADD:该模式下会把新的语音任务放到语音任务之后,等前面的语音任务执行完了才会执行新的语音任务。

  • 将朗读的的声音记录成音频文件
synthesizeToFile(CharSequence text,Bundle params,File file,String utteranceId);
     private void sayHello() {
         String hello ="Hellow";
         //TextToSpeech的speak方法有两个重载。
         // 执行朗读的方法
         //speak(CharSequence text,int queueMode,Bundle params,String utteranceId);
         // 将朗读的的声音记录成音频文件
         //synthesizeToFile(CharSequence text,Bundle params,File file,String utteranceId);
         //第二个参数queueMode用于指定发音队列模式,两种模式选择
         //(1)TextToSpeech.QUEUE_FLUSH:该模式下在有新任务时候会清除当前语音任务,执行新的语音任务
         //(2)TextToSpeech.QUEUE_ADD:该模式下会把新的语音任务放到语音任务之后,
         //等前面的语音任务执行完了才会执行新的语音任务

         mTts.speak(hello,
             TextToSpeech.QUEUE_FLUSH,
             null);
     }

4.记得利用Activity的生命周期中将其关闭

     @Override
     public void onDestroy() {
         // 生命周期中结束
         if (mTts != null) {
             mTts.stop();
             mTts.shutdown();
         }

         super.onDestroy();
     }

源码

SpeechActivity.java

public class SpeechActivity extends Activity implements TextToSpeech.OnInitListener {

     private static final String TAG = "SpeechDemo";

     private TextToSpeech mTts;
     private Button mButton;

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.text_to_speech);

         //初始化语音。这是一个异步操作。初始化完成后调用oninitListener(第二个参数)。
         mTts = new TextToSpeech(this, this);

         mButton = (Button) findViewById(R.id.again_button);
        //触发
         mButton.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 sayHello();
             }
         });
     }

     @Override
     public void onDestroy() {
         // 生命周期中结束
         if (mTts != null) {
             mTts.stop();
             mTts.shutdown();
         }

         super.onDestroy();
     }

     // 实现TextToSpeech.OnInitListener.
     public void onInit(int status) {
         if (status == TextToSpeech.SUCCESS) {
             //设置首选语言为中文,注意,语言可能是不可用的,结果将指示此
             int result = mTts.setLanguage(Locale.CHINA);
             if (result == TextToSpeech.LANG_MISSING_DATA ||
                 result == TextToSpeech.LANG_NOT_SUPPORTED) {
                 //语言数据丢失或不支持该语言。
                 Log.e(TAG, "语言数据丢失或不支持该语言");
             } else {
                 //检查文档中其他可能的结果代码。
                 // 例如,语言可能对区域设置可用,但对指定的国家和变体不可用
                 // TTS引擎已成功初始化。
                 // 允许用户按下按钮让应用程序再次发言。
                 mAgainButton.setEnabled(true);
             }
         } else {
             // 初始化失败
             Log.e(TAG, "初始化失败");
         }
     }

     private void sayHello() {
         String hello ="计蒙不吃鱼";
         //TextToSpeech的speak方法有两个重载。
         // 执行朗读的方法
         //speak(CharSequence text,int queueMode,Bundle params,String utteranceId);
         // 将朗读的的声音记录成音频文件
         //synthesizeToFile(CharSequence text,Bundle params,File file,String utteranceId);
         //第二个参数queueMode用于指定发音队列模式,两种模式选择
         //(1)TextToSpeech.QUEUE_FLUSH:该模式下在有新任务时候会清除当前语音任务,执行新的语音任务
         //(2)TextToSpeech.QUEUE_ADD:该模式下会把新的语音任务放到语音任务之后,
         //等前面的语音任务执行完了才会执行新的语音任务

         mTts.speak(hello,
             TextToSpeech.QUEUE_FLUSH,
             null);
     }

 }

text_to_speech.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <Button android:id="@+id/again_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:enabled="false" />
</LinearLayout>