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下載下傳