您的位置:首页 > 其它

科大讯飞语音合成Demo讲讲(做做玩,超简)

2014-03-08 16:17 453 查看
android手机上TTS功能有时添加到手机应用中会给手机的应用用户体验很不错的提高。比如懒人听书,打车软件的语音播放。都很好的解放了用户的双手带来便捷的操作体感。今天就闲着没事随意做了一个小Demo。不过功能上简单,代码基本没有规范。有时间我会将他更改过来!TTs我没有用android系统自带的因为很多国内的手机厂商直接就将TTS功能给剔出了。并且对中文语音的支持不太好。所以我采用了科大讯飞的API进行了语音开发。1.首先你需要去科大讯飞的官网去下载开发文档和SDK同时他们会为你提供一个简单的DEMO。文档和SDK都到手了开发就容易好多了。网址:http://open.voicecloud.cn/index.php/manage/sdk2.然后就是想其中导入夹包。科大讯飞的jar包有三种和多个平台的不同版本。我采用的是语音+的sdk。向android项目中导入jar的详细过程相信大家都已经熟悉了在此不再多说。对了要导入的jar是speachAPI.jar。3.然后直接上代码:布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="39dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:layout_marginLeft="105dp"
        android:layout_marginTop="96dp"
        android:text="Button" />

</RelativeLayout>
然后呢,我为了省省事我就写了个类吧TTs的这类东西给写到了一个Test类中:
package com.example.haha;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.RemoteException;

import com.iflytek.speech.ErrorCode;
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 Test {
	private SpeechSynthesizer mTts;          //语音合成对象

	public Test(){}
	public Test(Context context){
		init(context);
	}
	public void init(Context context){
		//初始化语音对象
		mTts = new SpeechSynthesizer(context, mTtsInitListener);
		//参数初始化
		setParam();	
	}
	//播放函数
	public void play(String str){
		String text = str;
		setParam();
		// 设置参数
		int code = mTts.startSpeaking(text, mTtsListener);
		if (code != 0) {
			System.out.println("start speak error : " + code);
		} else
			System.out.println("start speak success.");
	}
	//关闭播放
	public void Cancel(){
		mTts.stopSpeaking(mTtsListener);
		System.out.println("关闭播放!!!");
	}
	//暂停
	public void pause(){
		mTts.pauseSpeaking(mTtsListener);
	}
	
	//继续
	public void resume(){
		mTts.resumeSpeaking(mTtsListener);
		System.out.println();
	}	    
	/**
	 * 参数设置
	 * @param param
	 * @return 
	 */
	private void setParam(){
		mTts.setParameter(SpeechConstant.ENGINE_TYPE, "local");
		
		
		mTts.setParameter(SpeechSynthesizer.VOICE_NAME,"xiaoyan");

		mTts.setParameter(SpeechSynthesizer.SPEED, "50");
		
		mTts.setParameter(SpeechSynthesizer.PITCH, "50");
		
		mTts.setParameter(SpeechSynthesizer.VOLUME, "50");
	}
	
	public void destroy(){
		 mTts.stopSpeaking(mTtsListener);
		 mTts.destory();
	}
	
	/**
     * 初期化监听。
     */
	InitListener mTtsInitListener = new InitListener() {
		@Override
		public void onInit(ISpeechModule arg0, int code) {
        	if (code == ErrorCode.SUCCESS) {
        		System.out.println("初始化成功!!!");
        	}
		}
    };
    /**
//     * 合成回调监听。
//     */
    SynthesizerListener mTtsListener = new SynthesizerListener.Stub() {
        @Override
        public void onBufferProgress(int progress) throws RemoteException {
        	
        	 System.out.println("onBufferProgress :" + progress);
        }

        @Override
        public void onCompleted(int code) throws RemoteException {
            System.out.println("onCompleted code =" + code);
        }

        @Override
        public void onSpeakBegin() throws RemoteException {
            System.out.println("onSpeakBegin");
        }

        @Override
        public void onSpeakPaused() throws RemoteException {
        	 System.out.println("onSpeakPaused.");
        }

        @Override
        public void onSpeakProgress(int progress) throws RemoteException {
        	System.out.println("onSpeakProgress :" + progress);
        }

        @Override
        public void onSpeakResumed() throws RemoteException {
        	System.out.println("onSpeakResumed");
        }
    };
	

}
好了具体就不在介绍了还有一个Acitivity:
package com.example.haha;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
	
	private EditText edt;
	private Button but;
	private Test test;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		edt = (EditText) findViewById(R.id.editText1);
		but = (Button) findViewById(R.id.button1);
		Context c = this.getApplicationContext();
		//SpeechUtility.getUtility(this).setAppid("4d6774d0");
		test = new Test(c);				
		but.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View arg0) {
				test.play(edt.getText().toString());
			}
		
		});
		
	}

    protected void onDestroy() {
        super.onDestroy();
        test.destroy();
    }

}
好了代码就这些了是不是很简单,接着来张图吧这些真的没了。不过要想让应用发声不过还要一个插件,讯飞语音+插件。这部分代码官方的Demo上有我就没有写。回头加上吧。

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: