说明:这个可以实现离线语音合成与联网语音合成
MainActivity.java代码如下:
package com.chen.test16;
import android.app.Activity;
import android.os.Bundle;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.iflytek.speech.ISpeechModule;
import com.iflytek.speech.InitListener;
import com.iflytek.speech.SpeechConstant;
import com.iflytek.speech.SpeechSynthesizer;
import com.iflytek.speech.SpeechUtility;
import com.iflytek.speech.SynthesizerListener;
public class MainActivity extends Activity {
private TextView textView;
private Button button;
SpeechSynthesizer mTts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new ButtonListener());
if (SpeechUtility.getUtility(this).queryAvailableEngines()
== null || SpeechUtility.getUtility(this).
queryAvailableEngines().length <= 0)
{
textView.setText("download");
}
SpeechUtility.getUtility(this).setAppid("51b6eedb");
mTts =new SpeechSynthesizer(this,mTtsInitListener);
}
class ButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mTts.setParameter(SpeechConstant.ENGINE_TYPE, "local");
mTts.setParameter(SpeechSynthesizer.VOICE_NAME, "xiaoyan");
mTts.setParameter(SpeechSynthesizer.SPEED, "50");
mTts.setParameter(SpeechSynthesizer.PITCH, "50");
int code = mTts.startSpeaking("科大讯飞语音合成", mTtsListener);
}
}
private InitListener mTtsInitListener = new InitListener() {
@Override
public void onInit(ISpeechModule arg0, int code) {
// TODO Auto-generated method stub
}
};
SynthesizerListener mTtsListener = new SynthesizerListener.Stub() {
@Override
public void onSpeakResumed() throws RemoteException {
// TODO Auto-generated method stub
}
@Override
public void onSpeakProgress(int progress) throws RemoteException {
// TODO Auto-generated method stub
}
@Override
public void onSpeakPaused() throws RemoteException {
// TODO Auto-generated method stub
}
@Override
public void onSpeakBegin() throws RemoteException {
// TODO Auto-generated method stub
}
@Override
public void onCompleted(int code) throws RemoteException {
// TODO Auto-generated method stub
}
@Override
public void onBufferProgress(int progress) throws RemoteException {
// TODO Auto-generated method stub
}
};
}
注:首先在/libs 目录下粘贴SpeechApi.jar文件
然后在工程中导入已存在的SpeechApi.jar文件