简单的语音合成与语音识别(科大讯飞)

首先导入科大讯飞工具包html

MainActivity源代码:java

package com.example.viocedemo;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


import com.iflytek.cloud.speech.RecognizerResult;
import com.iflytek.cloud.speech.SpeechConstant;
import com.iflytek.cloud.speech.SpeechError;
import com.iflytek.cloud.speech.SpeechListener;
import com.iflytek.cloud.speech.SpeechSynthesizer;
import com.iflytek.cloud.speech.SpeechUser;
import com.iflytek.cloud.speech.SynthesizerListener;
import com.iflytek.cloud.ui.RecognizerDialog;
import com.iflytek.cloud.ui.RecognizerDialogListener;


public class MainActivity extends Activity implements OnClickListener,
SynthesizerListener {
private EditText editText;
private Button button1;
private Button button2;
// 合成对象
private SpeechSynthesizer speechSynthesizer;
// 识别窗口
private RecognizerDialog recognizerDialog;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


// appid换成本身申请的
SpeechUser.getUser().login(MainActivity.this, null, null,
"appid=12345678", listener);


init();
setParam();


}


/**
* 初始化UI
*/
public void init() {
editText = (EditText) findViewById(R.id.editText1);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}


/**
* 初始化语音类别
*/
public void setParam() {
speechSynthesizer = SpeechSynthesizer.createSynthesizer(this);
speechSynthesizer.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");
speechSynthesizer.setParameter(SpeechConstant.SPEED, "50");
speechSynthesizer.setParameter(SpeechConstant.VOLUME, "50");
speechSynthesizer.setParameter(SpeechConstant.PITCH, "50");
}


/**
* 识别语音的弹出框
*/
public void setDialog() {
recognizerDialog = new RecognizerDialog(this);
recognizerDialog.setParameter(SpeechConstant.DOMAIN, "iat");
recognizerDialog.setParameter(SpeechConstant.SAMPLE_RATE, "16000");
editText.setText(null);
// 显示Dialog
recognizerDialog.setListener(dialogListener);
recognizerDialog.show();
}


/**
* 识别回调监听器
*/
private RecognizerDialogListener dialogListener = new RecognizerDialogListener() {
// 识别结果回调
@Override
public void onResult(RecognizerResult arg0, boolean arg1) {
// TODO Auto-generated method stub
String text = JsonParser.parseIatResult(arg0.getResultString());
editText.append(text);
editText.setSelection(editText.length());
}


// 识别结束回调
@Override
public void onError(SpeechError arg0) {
// TODO Auto-generated method stub


}
};


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:// 语音播放按钮
String text = editText.getText().toString();
speechSynthesizer.startSpeaking(text, this);
break;
case R.id.button2:// 语音识别
setDialog();
break;
default:
break;
}
}


// 缓冲进度回调通知
@Override
public void onBufferProgress(int arg0, int arg1, int arg2, String arg3) {
// TODO Auto-generated method stub


}


// 结束回调
@Override
public void onCompleted(SpeechError arg0) {
// TODO Auto-generated method stub

}


// 开始播放
@Override
public void onSpeakBegin() {
// TODO Auto-generated method stub
}


// 暂停播放
@Override
public void onSpeakPaused() {
// TODO Auto-generated method stub

}


// 播放进度
@Override
public void onSpeakProgress(int arg0, int arg1, int arg2) {
// TODO Auto-generated method stub

}


// 继续播放
@Override
public void onSpeakResumed() {
// TODO Auto-generated method stub

}


/**
* 通用回调接口
*/
private SpeechListener listener = new SpeechListener() {


// 消息回调
@Override
public void onEvent(int arg0, Bundle arg1) {
// TODO Auto-generated method stub


}


// 数据回调
@Override
public void onData(byte[] arg0) {
// TODO Auto-generated method stub


}


// 结束回调(没有错误)
@Override
public void onCompleted(SpeechError arg0) {
// TODO Auto-generated method stub

}
};


}


JsonParser类源代码:
android

package com.example.viocedemo;


import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;


import android.text.TextUtils;


//import com.iflytek.speech.ErrorCode;
//import com.iflytek.speech.SpeechError;
/**
 * 对云端返回的Json结果进行解析
 * @author iFlytek
 * @since 20131211
 */
public class JsonParser {

/**
* 听写结果的Json格式解析
* @param json
* @return
*/
public static String parseIatResult(String json) {
if(TextUtils.isEmpty(json))
return "";

StringBuffer ret = new StringBuffer();
try {
JSONTokener tokener = new JSONTokener(json);
JSONObject joResult = new JSONObject(tokener);


JSONArray words = joResult.getJSONArray("ws");
for (int i = 0; i < words.length(); i++) {
JSONArray items = words.getJSONObject(i).getJSONArray("cw");
JSONObject obj = items.getJSONObject(0);
ret.append(obj.getString("w"));
}
} catch (Exception e) {
e.printStackTrace();
} 
return ret.toString();
}

}


UI布局:json


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="250dip"
        android:gravity="top"
        android:ems="10" >


        <requestFocus />
    </EditText>


    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="语音播放" />


    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="语音听写" />


</LinearLayout>

源代码下载