天天看点

Android笔记之 TTS中文发音threadid=1: thread exiting with uncaught exception (group=0x4001d800)

1、TTS 就是 Text to Speech ,把文本内容变为语音。 谷歌在Android 1.6 开始就支持TTS 了,但是可惜,只是支持英语法语德语等五种语言,唯独丫丫的木有我们中文。 所以,我们只能另外自己开发中文语音包程序。

目前主要有以下几种中文TTS 。  (1)开源项目  eyes-free   ,链接是:    http://code.google.com/p/eyes-free/   在手机上安装了 eyes-free   提供的      TTS Service Extended.apk 文件后,就可以在手机-设置-语音输入-中设置是eSpeak输入为默认的语音输入。

实际效果没有测试,网上有其他网友测试说效果很差。具体如何开发,请参考这篇文章: http://blog.csdn.net/ichliebephone/article/details/6373184

(2)科大讯飞 这个比较出名了,因为苹果的语音合成让它火了一把,它也是国内语音合成方面做得比较好的一个公司。 之前提供安卓开发的中文语音引擎。但是现在找不到了。

(3)手说TTS  手说TTS,是Android平台下的中文语音引擎,提供了中文文本到语音的转换。

使用手说TTS进行中文文本的朗读,包括中文简繁体、阿拉伯数字、英文字母及一些符号的混读。并且处理了中文的多音字和音调转换等问题。个人工作室所做的。因为他提供了比较详细的二次开发接口,所以我下面就是针对他做了一个中文TTS开发。实际测试,效果差钱人意吧,普通话真的不是很好。此外,只有最新版的才有连贯功能,以前的没有,纠结我半天。

下面给出调试过程和源码。

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

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

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

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

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

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

第三步,编写代码

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.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.shoushuo.android.tts.ITts;


/**

 * @version 1.0
 */
public class Speech extends Activity
{ 
	
  private ITts ttsService; 

     private boolean ttsBound;
     private ServiceConnection connection = new ServiceConnection() { 
      
       @Override 
       public void onServiceConnected(ComponentName className, IBinder iservice) { 
         ttsService = ITts.Stub.asInterface(iservice); 
         ttsBound = true; 
      
      //在应用第一个使用TTS 的地方,调用下面的initialize方法,比如如果有 
     //两个Activity都使用手说TTS,则第二个Activity在此不需要再调用。 
     try { 
           ttsService.initialize(); 
     } catch (RemoteException e) { 
      e.printStackTrace();
      setTitle("出错啦");
     } 
       } 
      
       @Override 
       public void onServiceDisconnected(ComponentName arg0) { 
         ttsService = null; 
         ttsBound = false; 
       } 
     }; 
 private EditText edt;
	
 private Button press; 

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

         this.press = ( Button ) findViewById( R.id.speech );
         edt = (EditText)findViewById(R.id.txt);
         //给Button 添加事件监听器Button.OnClickListener()
       
         
         //处理事件
       press.setOnClickListener(new OnClickListener()
         {

             @Override
             public void onClick( View source)
             {
              
              try {
      ttsService.speak("欢迎小朋友",0);
     } catch (RemoteException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
             }
         } );

 }
	
  @Override 
     protected void onStart() { 
       super.onStart(); 
       if (!ttsBound ) { 
         String actionName = "com.shoushuo.android.tts.intent.action.InvokeTts"; 
         Intent intent = new Intent(actionName); 
         this.bindService(intent, connection, Context.BIND_AUTO_CREATE); 
       } 
     } 
    
   @Override 
     protected void onDestroy () { 
       if (ttsBound ) { 
         ttsBound = false; 
         this.unbindService(connection); 
       } 
   super. onDestroy (); 
     }  
	
}
           

记得用最新版的手说TTS apk 程序,因为旧版没有连贯朗读的功能,一字字地很生硬。

此外,特别注意一点,就是导入 shoushuo开发包的时候,如果你出现了如下的错误,

threadid=1: thread exiting with uncaught exception (group=0x4001d800)

解决办法:  buildpath->Order and Export->select All !!!

参考资料 

  http://blog.csdn.net/ichliebephone/article/details/6373184

http://www.cnblogs.com/linjiqin/archive/2011/11/01/2231598.html 

http://www.eoeandroid.com/thread-308805-1-1.html

DEMO下载