##一、将libs文件夹移至Android Studio的Project的根目录下
将科大讯飞提供的SDK中的libs文件夹直接拖拽至Android Studio的Project的根目录下
##二、导入xxx.jar文件
其实.jar文件已经存在于libs文件夹下了,不过此时它并不会随工程一起编译,所以我们还需要做一些设置。
设置方法很简单,如下图所示:
##三、导入xxx.so文件
其实.so文件已经存在于libs文件夹下了,不过此时它并不会随工程一起编译,所以我们还需要做一些设置。
设置方法很简单在build.gradle中添加如下代码即可
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
文件:App.java
// 创建全局实例
SpeechUtility.createUtility(App.this, "appid=" + getString(R.string.app_id));
// 关闭MSC Log
Setting.setShowLog(false);
文件:MainActivity.java
//语音控件
private SpeechUnderstander mSpeechUnderstander; // 语义理解对象(语音到语义)
private TextUnderstander mTextUnderstander; // 语义理解对象(文本到语义)
private SpeechRecognizer mIat; // 语音听写对象
private SpeechSynthesizer mTts; // 语音合成对象
/*
* 初始化语音控件
*/
private void initVoice() {
// 初始化语义理解对象(语音到语义)
mSpeechUnderstander = SpeechUnderstander.createUnderstander(MainActivity.this, mSpeechUdrInitListener);
// 初始化语义理解对象(文本到语义)
mTextUnderstander = TextUnderstander.createTextUnderstander(MainActivity.this, textUnderstanderListener);
// 初始化语音合成对象
mTts = SpeechSynthesizer.createSynthesizer(MainActivity.this, mTtsInitListener);
// 初始化语音识别对象
mIat = SpeechRecognizer.createRecognizer(MainActivity.this, mInitListener);
// 语音识别参数设置
setRecognizerParam();
// 语音合成参数设置
setTtsParam();
}
/***start*************************************语音控件初始化回调**************************************************/
/**
* 初始化语义理解监听器(语音到语义)
*/
private InitListener mSpeechUdrInitListener = new InitListener() {
@Override
public void onInit(int code) {
Log.d(TAG, "speechUnderstanderListener init() code = " + code);
if (code != ErrorCode.SUCCESS) {
showTip("初始化失败,错误码:"+code);
}
}
};
/**
* 初始化监听器(文本到语义)
*/
private InitListener textUnderstanderListener = new InitListener() {
@Override
public void onInit(int code) {
Log.d(TAG, "textUnderstanderListener init() code = " + code);
if (code != ErrorCode.SUCCESS) {
showTip("初始化失败,错误码:"+code);
}
}
};
/**
* 初始化语音识别监听器
*/
private InitListener mInitListener = new InitListener() {
@Override
public void onInit(int code) {
Log.d(TAG, "SpeechRecognizer init() code = " + code);
if (code != ErrorCode.SUCCESS) {
showTip("初始化失败,错误码:" + code);
}
}
};
/**
* 初始化语音合成监听
*/
private InitListener mTtsInitListener = new InitListener() {
@Override
public void onInit(int code) {
Log.d(TAG, "InitListener init() code = " + code);
if (code != ErrorCode.SUCCESS) {
showTip("初始化失败,错误码:"+code);
} else {
// 初始化成功,之后可以调用startSpeaking方法
// 注:有的开发者在onCreate方法中创建完合成对象之后马上就调用startSpeaking进行合成,
// 正确的做法是将onCreate中的startSpeaking调用移至这里
// mTts.startSpeaking("小朋友", mTtsListener);
}
}
};
/***end*************************************语音控件初始化回调****************************************************/
##五、语音控件的使用
/**
* 使用语音合成
*/
mTts.startSpeaking("需要合称为语音的文本", mTtsListener);
/**
* 使用语音识别
*/
ret = mIat.startListening(mRecognizerListener);
if (ret != ErrorCode.SUCCESS) {
showTip("听写失败,错误码:" + ret);
} else {
showTip("请开始说话…");
}
/**
* 使用语义理解(语音到语义)
*/
if(mSpeechUnderstander.isUnderstanding()){// 开始前检查状态
mSpeechUnderstander.stopUnderstanding();
mMyHandler.obtainMessage(0, "停止录音").sendToTarget();
}else {
ret = mSpeechUnderstander.startUnderstanding(mSpeechUnderstanderListener);
if(ret != 0){
showTip("语义理解失败,错误码:" + ret);
}else {
showTip("请开始说话…");
}
}
/**
* 使用语义理解(文本到语义)
*/
if(mTextUnderstander.isUnderstanding()){
mTextUnderstander.cancel();
showTip("取消");
}else {
ret = mTextUnderstander.understandText("需要语义理解的文本", textListener);
if(ret != 0)
{
showTip("语义理解失败,错误码:"+ ret);
}
}
/***start*************************************语音控件回调****************************************************/
/**
* 语音合成回调
*/
private SynthesizerListener mTtsListener = new SynthesizerListener() {
@Override
public void onSpeakResumed() {
// TODO Auto-generated method stub
}
@Override
public void onSpeakProgress(int arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onSpeakPaused() {
// TODO Auto-generated method stub
}
@Override
public void onSpeakBegin() {
System.out.println("开始播放");
}
@Override
public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
}
@Override
public void onCompleted(SpeechError error) {
System.out.println("播放完成");
if (atEnterWakeup) {
atEnterWakeup = false;
new SemanticUnderstand("WithoutHello").start();
}else {
new SemanticUnderstand(null).start();
}
}
@Override
public void onBufferProgress(int arg0, int arg1, int arg2, String arg3) {
// TODO Auto-generated method stub
}
};
/**
* 语音识别回调
*/
private RecognizerListener mRecognizerListener = new RecognitionListener() {
@Override
public void onRmsChanged(float rmsdB) {
// TODO Auto-generated method stub
}
@Override
public void onResults(Bundle results) {
// TODO Auto-generated method stub
}
@Override
public void onReadyForSpeech(Bundle params) {
// TODO Auto-generated method stub
}
@Override
public void onPartialResults(Bundle partialResults) {
// TODO Auto-generated method stub
}
@Override
public void onEvent(int eventType, Bundle params) {
// TODO Auto-generated method stub
}
@Override
public void onError(int error) {
// TODO Auto-generated method stub
}
@Override
public void onEndOfSpeech() {
// TODO Auto-generated method stub
}
@Override
public void onBufferReceived(byte[] buffer) {
// TODO Auto-generated method stub
}
@Override
public void onBeginningOfSpeech() {
// TODO Auto-generated method stub
}
};
/**
* 语义理解回调(语音到语义)
*/
private SpeechUnderstanderListener mSpeechUnderstanderListener = new SpeechUnderstanderListener() {
@Override
public void onVolumeChanged(int arg0, byte[] arg1) {
// TODO Auto-generated method stub
}
@Override
public void onResult(UnderstanderResult arg0) {
// TODO Auto-generated method stub
}
@Override
public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
// TODO Auto-generated method stub
}
@Override
public void onError(SpeechError arg0) {
// TODO Auto-generated method stub
}
@Override
public void onEndOfSpeech() {
// TODO Auto-generated method stub
}
@Override
public void onBeginOfSpeech() {
// TODO Auto-generated method stub
}
};
/**
*语音理解回调(文本到语义)
*/
private TextUnderstanderListener textListener= new TextUnderstanderListener() {
@Override
public void onResult(UnderstanderResult arg0) {
// TODO Auto-generated method stub
}
@Override
public void onError(SpeechError arg0) {
// TODO Auto-generated method stub
}
};
/***end*************************************语音控件回调****************************************************/