說明:這個可以實現離線語音合成與連網語音合成
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;@Overrideprotected 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 {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubmTts.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() {@Overridepublic void onInit(ISpeechModule arg0, int code) {// TODO Auto-generated method stub}};SynthesizerListener mTtsListener = new SynthesizerListener.Stub() {@Overridepublic void onSpeakResumed() throws RemoteException {// TODO Auto-generated method stub}@Overridepublic void onSpeakProgress(int progress) throws RemoteException {// TODO Auto-generated method stub}@Overridepublic void onSpeakPaused() throws RemoteException {// TODO Auto-generated method stub}@Overridepublic void onSpeakBegin() throws RemoteException {// TODO Auto-generated method stub}@Overridepublic void onCompleted(int code) throws RemoteException {// TODO Auto-generated method stub}@Overridepublic void onBufferProgress(int progress) throws RemoteException {// TODO Auto-generated method stub}};}
註:首先在/libs 目錄下粘貼SpeechApi.jar檔案
然後在工程中匯入已存在的SpeechApi.jar檔案