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 version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><EditText android:id="@+id/edtSpeectText"android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="太阳从东边升起,慢慢的露出红彤彤的笑脸。"/><Button      android:id="@+id/btnSpeechGo"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="开始朗读"    android:onClick="speechText"    /></LinearLayout>

java代码:

package com.zhouzijing.android.demo;import com.shoushuo.android.tts.ITts;import com.shoushuo.android.tts.ITtsCallback;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.Handler;import android.os.IBinder;import android.os.Message;import android.os.RemoteException;import android.speech.tts.TextToSpeech;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class SpeechActivity extends Activity {private EditText edtSpeectText;private Button btnSpeechGo;private Context context;private ITts ttsService;private boolean ttsBound;/** * 定义Handler. */private final Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {Toast.makeText(context, " 我的话说完了 ", Toast.LENGTH_SHORT).show();btnSpeechGo.setEnabled(true);}};/** * 回调参数. */private final ITtsCallback ttsCallback = new ITtsCallback.Stub() {//朗读完毕.@Overridepublic void speakCompleted() throws RemoteException {handler.sendEmptyMessage(0);}};/** * tts服务连接. */private final ServiceConnection ttsConnection = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName arg0) {try {//注册回调参数ttsService.unregisterCallback(ttsCallback);} catch (RemoteException e) {e.printStackTrace();}ttsService = null;ttsBound = false;}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {ttsService = ITts.Stub.asInterface(service);ttsBound = true;try {//tts服务初始化ttsService.initialize();//撤销回调参数.ttsService.registerCallback(ttsCallback);} catch (RemoteException e) {}}};@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.speech_text);context = this;edtSpeectText = (EditText) findViewById(R.id.edtSpeectText);btnSpeechGo = (Button) findViewById(R.id.btnSpeechGo);}/** * 按钮:朗读. *  * @param v */public void speechText(View v) {v.setEnabled(false);try {ttsService.speak(edtSpeectText.getText().toString(),TextToSpeech.QUEUE_FLUSH);} catch (RemoteException e) {e.printStackTrace();}}@Overrideprotected void onDestroy() {if (ttsBound) {ttsBound = false;//撤销tts服务this.unbindService(ttsConnection);}super.onDestroy();}@Overrideprotected void onStart() {super.onStart();if (!ttsBound) {String actionName = "com.shoushuo.android.tts.intent.action.InvokeTts";Intent intent = new Intent(actionName);//绑定tts服务this.bindService(intent, ttsConnection, Context.BIND_AUTO_CREATE);}}}

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. android: 后台执行的定时任务
  3. Android(安卓)中文API
  4. android 反射应用
  5. Android(安卓)onCreateOptionsMenu的使用方法总结
  6. TimePicker点击上下方向键 java.lang.NullPointerException异常
  7. Android中文API(114)——TabWidget
  8. Android中动态刷新从服务器上获取的数据
  9. android adt jni开发步骤

随机推荐

  1. Android(安卓)ApiDemos示例解析(85):Grap
  2. Android(安卓)图片切换,ImageSwitcher 学
  3. Android 自定义文件路径选择器
  4. 让Cocos2d-x实现全屏等比缩放适配Android
  5. Android 平台上拍照/录像,图片/视频选择,编
  6. Android WebView中的JavaScript代码使用
  7. Android:No permission to write APN sett
  8. android控件详解----TextView
  9. Android 系统中 Location Service 的实现
  10. Android(安卓)Permission权限通知