第一步:安装手说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 ();
}
}
Android笔记之 TTS中文发音,布布扣,bubuko.com
原文:http://blog.csdn.net/linfeng24/article/details/36942349