Android 通过手说tts中文语音包实现中文朗读

Android 通过手说tts中文语音包实现中文朗读

 

 

关于手说tts中文语音包的详细资料可以查看官网 http://shoushuo.com/index.html

 

手说TTS,是Android平台下的中文语音引擎,提供了中文文本到语音的转换。
使用手说TTS进行中文文本的朗读,包括中文简繁体、阿拉伯数字、英文字母及一些符号的混读。并且处理了中文的多音字和音调转换等问题。
开发人员可以使用手说TTS来开发Android平台下需要中文语音的应用程序。

 

开发准备:

 

第一步:安装手说TTS安装包

 

从官网 http://shoushuo.com/sstts.html 下载手说TTS安装包:ShoushuoTTS.apk 。

 

安装到真实手机或者手机模拟器中。

 

第二步:下载手说TTS客户类库包

 

下载手说TTS客户类库包:shoushuotts.jar 。

将该jar文件引入到你的应用中。

 

第二步:demo实现

xml文件

Xml代码   收藏代码
  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/edtSpeectText"  
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="太阳从东边升起,慢慢的露出红彤彤的笑脸。"  
  12.     />  
  13.   
  14. <Button    
  15.     android:id="@+id/btnSpeechGo"  
  16.     android:layout_width="fill_parent"   
  17.     android:layout_height="wrap_content"   
  18.     android:text="开始朗读"  
  19.     android:onClick="speechText"  
  20.     />  
  21. </LinearLayout>  
 

java代码:

Java代码   收藏代码
  1. package com.zhouzijing.android.demo;  
  2.   
  3. import com.shoushuo.android.tts.ITts;  
  4. import com.shoushuo.android.tts.ITtsCallback;  
  5. import android.app.Activity;  
  6. import android.content.ComponentName;  
  7. import android.content.Context;  
  8. import android.content.Intent;  
  9. import android.content.ServiceConnection;  
  10. import android.os.Bundle;  
  11. import android.os.Handler;  
  12. import android.os.IBinder;  
  13. import android.os.Message;  
  14. import android.os.RemoteException;  
  15. import android.speech.tts.TextToSpeech;  
  16. import android.view.View;  
  17. import android.widget.Button;  
  18. import android.widget.EditText;  
  19. import android.widget.Toast;  
  20.   
  21. public class SpeechActivity extends Activity {  
  22.   
  23.     private EditText edtSpeectText;  
  24.     private Button btnSpeechGo;  
  25.     private Context context;  
  26.     private ITts ttsService;  
  27.     private boolean ttsBound;  
  28.   
  29.     /** 
  30.      * 定义Handler. 
  31.      */  
  32.     private final Handler handler = new Handler() {  
  33.         @Override  
  34.         public void handleMessage(Message msg) {  
  35.             Toast.makeText(context, " 我的话说完了 ", Toast.LENGTH_SHORT).show();  
  36.             btnSpeechGo.setEnabled(true);  
  37.         }  
  38.     };  
  39.   
  40.     /** 
  41.      * 回调参数. 
  42.      */  
  43.     private final ITtsCallback ttsCallback = new ITtsCallback.Stub() {  
  44.         //朗读完毕.  
  45.         @Override  
  46.         public void speakCompleted() throws RemoteException {  
  47.             handler.sendEmptyMessage(0);  
  48.         }  
  49.     };  
  50.   
  51.     /** 
  52.      * tts服务连接. 
  53.      */  
  54.     private final ServiceConnection ttsConnection = new ServiceConnection() {  
  55.         @Override  
  56.         public void onServiceDisconnected(ComponentName arg0) {  
  57.             try {  
  58.                 //注册回调参数  
  59.                 ttsService.unregisterCallback(ttsCallback);  
  60.             } catch (RemoteException e) {  
  61.                 e.printStackTrace();  
  62.             }  
  63.             ttsService = null;  
  64.             ttsBound = false;  
  65.         }  
  66.         @Override  
  67.         public void onServiceConnected(ComponentName name, IBinder service) {  
  68.             ttsService = ITts.Stub.asInterface(service);  
  69.             ttsBound = true;  
  70.             try {  
  71.                 //tts服务初始化  
  72.                 ttsService.initialize();  
  73.                 //撤销回调参数.  
  74.                 ttsService.registerCallback(ttsCallback);  
  75.             } catch (RemoteException e) {  
  76.             }  
  77.         }  
  78.     };  
  79.   
  80.     @Override  
  81.     public void onCreate(Bundle savedInstanceState) {  
  82.         super.onCreate(savedInstanceState);  
  83.         setContentView(R.layout.speech_text);  
  84.         context = this;  
  85.         edtSpeectText = (EditText) findViewById(R.id.edtSpeectText);  
  86.         btnSpeechGo = (Button) findViewById(R.id.btnSpeechGo);  
  87.     }  
  88.   
  89.     /** 
  90.      * 按钮:朗读. 
  91.      *  
  92.      * @param v 
  93.      */  
  94.     public void speechText(View v) {  
  95.         v.setEnabled(false);  
  96.         try {  
  97.             ttsService.speak(edtSpeectText.getText().toString(),  
  98.                     TextToSpeech.QUEUE_FLUSH);  
  99.         } catch (RemoteException e) {  
  100.             e.printStackTrace();  
  101.         }  
  102.     }  
  103.   
  104.     @Override  
  105.     protected void onDestroy() {  
  106.         if (ttsBound) {  
  107.             ttsBound = false;  
  108.             //撤销tts服务  
  109.             this.unbindService(ttsConnection);  
  110.         }  
  111.         super.onDestroy();  
  112.     }  
  113.   
  114.     @Override  
  115.     protected void onStart() {  
  116.         super.onStart();  
  117.         if (!ttsBound) {  
  118.             String actionName = "com.shoushuo.android.tts.intent.action.InvokeTts";  
  119.             Intent intent = new Intent(actionName);  
  120.             //绑定tts服务  
  121.             this.bindService(intent, ttsConnection, Context.BIND_AUTO_CREATE);  
  122.         }  
  123.     }  
  124.   
  125. }  
 原文:http://stephen830.iteye.com/blog/1183326
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值