2013年4月14日 星期日

Android(朗读文本) TextToSpeech的使用


使用TextToSpeech 可以朗读文本,要先初始化TextToSpeech 对象,通过实现TextToSpeech.OnInitListener接口来检测初始化状态成功与否(设置语言等动作是否成功),初始化成功后,才可以使用,用完该对象后,要调用shutdown方法,释放TextToSpeech (TTS)引擎占用的资源
Java代码  收藏代码
  1. package com.zhou.activity;  
  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.   
  13. public class TextToSpeechActivity extends Activity implements TextToSpeech.OnInitListener{  
  14.       private static final String TAG = "TextToSpeechDemo";  
  15.   
  16.         private TextToSpeech mTts;  
  17.         private Button mAgainButton;  
  18.   
  19.         @Override  
  20.         public void onCreate(Bundle savedInstanceState) {  
  21.             super.onCreate(savedInstanceState);  
  22.             setContentView(R.layout.text_to_speech);  
  23.   
  24.             // 初始化TextToSpeech对象. 是一个异步操作.  
  25.             // 初始化监听对象,在完成初始化时调用  
  26.             mTts = new TextToSpeech(this,this);  
  27.             //取得按钮  
  28.             mAgainButton = (Button) findViewById(R.id.again_button);  
  29.             //为按钮设置单击事件  
  30.             mAgainButton.setOnClickListener(new View.OnClickListener() {  
  31.                 public void onClick(View v) {  
  32.                     sayHello();  
  33.                 }  
  34.             });  
  35.         }  
  36.   
  37.         @Override  
  38.         public void onDestroy() {  
  39.             if (mTts != null) {  
  40.                 //停止TextToSpeech  
  41.                 mTts.stop();  
  42.                 //释放TextToSpeech占用的资源  
  43.                 mTts.shutdown();  
  44.             }  
  45.             super.onDestroy();  
  46.         }  
  47.   
  48.         //实现TextToSpeech.OnInitListener接口.  
  49.         public void onInit(int status) {  
  50.             //状态成功  
  51.             if (status == TextToSpeech.SUCCESS) {  
  52.                //设置语言  
  53.                 int result = mTts.setLanguage(Locale.US);  
  54.                 //(我试了中文,不好使????)  
  55. //              int result = mTts.setLanguage(Locale.CHINA);  
  56.                 
  57.                 if (result == TextToSpeech.LANG_MISSING_DATA ||  
  58.                     result == TextToSpeech.LANG_NOT_SUPPORTED) {  
  59.                    // 如果不支持该语言  
  60.                     Log.e(TAG, "Language is not available.");  
  61.                 } else {  
  62.                     // TTS 引擎已经成功初始化   
  63.                     //按钮设置为可点击  
  64.                     mAgainButton.setEnabled(true);  
  65.                     //发音朗读  
  66.                     sayHello();  
  67.                 }  
  68.             } else {  
  69.                 // 初始化失败  
  70.                 Log.e(TAG, "Could not initialize TextToSpeech.");  
  71.             }  
  72.         }  
  73.   
  74.         private static final Random RANDOM = new Random();  
  75.         private static final String[] HELLOS = {  
  76.           "Hello",  
  77.           "Salutations",  
  78.           "Greetings",  
  79.           "Howdy",  
  80.           "What's crack-a-lackin?",  
  81.           "That explains the stench!"  
  82.         };  
  83.         //尝试朗读中文,失败了,语言设置为中国的也不行  
  84. //      private static final String[] HELLOS = {  
  85. //            "中国",  
  86. //            "北京",  
  87. //            "上海",  
  88. //            "吉林",  
  89. //            "辽宁",  
  90. //            "北京欢迎你"  
  91. //          };  
  92.         private void sayHello() {  
  93.             // Select a random hello.  
  94.             int helloLength = HELLOS.length;  
  95.             String hello = HELLOS[RANDOM.nextInt(helloLength)];  
  96.             //朗读  
  97.             mTts.speak(hello,TextToSpeech.QUEUE_FLUSH, null);  
  98.         }  
  99. }  

沒有留言:

張貼留言

Related Posts Plugin for WordPress, Blogger...