1. 程式人生 > >訊飛語音合成例項2

訊飛語音合成例項2

說明:這個可以實現離線語音合成與聯網語音合成

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檔案