讯飞语音合成实例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文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值