Android进阶:Google自带语音播放功能实现

转自:http://blog.csdn.net/wanglong0537/article/details/6442999

在Android 中使用语音播放功能 只需要使用类 TextToSpeech ,该类实现了很多关于语音的功能,使用该类必须为其设置语言,现在支持五种语言,杯具的是不支持中文

 

实现很简单 不过首先要安装语言包 这个在设置--》语音输入和输出设置--》文字转语音设置

 

如下图

 

Android进阶:Google自带语音播放功能实现_第1张图片   Android进阶:Google自带语音播放功能实现_第2张图片

 

左边图中 安装语音数据  我这里已经安装成功了 所以是灰色的 如果没有安装这里就可以点 其他地方都是灰色的

 

安装文件4.28M  下载安装完成后就可以选择语言了 右图所示的五种语言 没有中文啊

 

下面来看实现 很简单

 

Android进阶:Google自带语音播放功能实现_第3张图片 

 

首先是layout文件:

 

view plain
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <EditText    
  8.     android:id="@+id/EditText01"  
  9.     android:layout_width="fill_parent"   
  10.     android:text="I hope so, because it's time to wake up."  
  11.     android:layout_height="wrap_content"   
  12.     />  
  13. <Button    
  14.     android:id="@+id/Button01"  
  15.     android:text="开始播放"  
  16.     android:layout_width="fill_parent"   
  17.     android:layout_height="wrap_content"   
  18.     />  
  19. </LinearLayout>  

 

就是播放EditText中的内容

 

Acitivity中

 

view plain
  1. import java.util.Locale;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.speech.tts.TextToSpeech;  
  5. import android.speech.tts.TextToSpeech.OnInitListener;  
  6. import android.util.Log;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11. public class VoisePlayDemo extends Activity {  
  12.     private TextToSpeech mSpeech;  
  13.     private Button btn;  
  14.     private EditText mEditText;  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.         btn = (Button) findViewById(R.id.Button01);  
  20.         mEditText = (EditText) findViewById(R.id.EditText01);  
  21.         btn.setEnabled(false);  
  22.         //创建TextToSpeech对象   
  23.         mSpeech = new TextToSpeech(thisnew OnInitListener() {  
  24.             @Override  
  25.             public void onInit(int status) {  
  26.                 if (status == TextToSpeech.SUCCESS) {  
  27.                     int result = mSpeech.setLanguage(Locale.US);  
  28.                     if (result == TextToSpeech.LANG_MISSING_DATA  
  29.                             || result == TextToSpeech.LANG_NOT_SUPPORTED) {  
  30.                         Log.e("bb""not use");  
  31.                     } else {  
  32.                         btn.setEnabled(true);  
  33.                     }  
  34.                 }  
  35.             }  
  36.         });  
  37.         btn.setOnClickListener(new OnClickListener() {  
  38.             @Override  
  39.             public void onClick(View v) {  
  40.                 mSpeech.speak(mEditText.getText().toString(),  
  41.                         TextToSpeech.QUEUE_FLUSH, null);  
  42.             }  
  43.         });  
  44.     }  
  45.     @Override  
  46.     protected void onDestroy() {  
  47.         if (mSpeech != null) {  
  48.             mSpeech.stop();  
  49.             mSpeech.shutdown();  
  50.         }  
  51.         super.onDestroy();  
  52.     }  

 

通过创建TextToSpeech类的实例 并在 onInit 初始化方法内判断语音加载是否成功 确实很简单了

 

就是不知道什么时候可以支持中文啊


你可能感兴趣的:(android,Google,layout,语言,button,encoding)