第一次接触语音识别还是2010年的时候,我帮一个靠语音算法创业的同学移植了他的语音引擎到一款嵌入式产品上去。
当时他做的引擎识别率还可以,但不够智能还需要找一些人来录音学习,也是那个时候知道了科大讯飞;
总结下刚刚移植的这个语音机器人(android平台)
因为用到了图灵的知识库,百度的语音识别和语音合成,所以需要去申请相应的key和下载相应的SDK库代码
1)去图灵注册申请一个key和secret
2) 去百度注册申请一个key和secret
同时只要在图灵上下载SDK包就可以,第一次移植不用去下载更新百度的库,不然可能会带来一些其他问题,代码也非常简单,有相应注释一看基本上都明白大概意思。
MainActivity.javapackage com.turing.demo;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;
import com.turing.androidsdk.HttpRequestListener;
import com.turing.androidsdk.RecognizeListener;
import com.turing.androidsdk.RecognizeManager;
import com.turing.androidsdk.TTSListener;
import com.turing.androidsdk.TTSManager;
import com.turing.androidsdk.TuringManager;
public class MainActivity extends Activity {
private final String TAG = MainActivity.class.getSimpleName();
private TTSManager mTtsManager;
private RecognizeManager mRecognizerManager;
private TuringManager mTuringManager;
private TextView mStatus;
/** 返回结果,开始说话 */
public static final int MSG_SPEECH_START = 0;
/** 开始识别 */
public static final int MSG_RECOGNIZE_RESULT = 1;
/** 开始识别 */
public static final int MSG_RECOGNIZE_START = 2;
/**
* 申请的turing的apikey(测试使用)
* **/
private final String TURING_APIKEY = "d975f8141aa550cea27b7f48dd50c48d";
/**
* 申请的secret(测试使用)
* **/
private final String TURING_SECRET = "4145a1cb5f92901b";
// 百度key(测试使用)
private final String BD_APIKEY = "ZC2NNfFUkg8rxgmVkfBC6ycX";
// 百度screte(测试使用)
private final String BD_SECRET = "9a98e53b2ef7339bf03793f0b53fc7e4";
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case MSG_SPEECH_START:
mStatus.setText("开始讲话:" + (String) msg.obj);
mTtsManager.startTTS((String) msg.obj);
break;
case MSG_RECOGNIZE_RESULT:
mStatus.setText("识别结果:" + msg.obj);
mTuringManager.requestTuring((String) msg.obj);
break;
case MSG_RECOGNIZE_START:
mStatus.setText("开始识别");
mRecognizerManager.startRecognize();
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStatus = (TextView) findViewById(R.id.tv_status);
init();
}
/**
* 初始化turingSDK、识别和tts
*
*/
private void init() {
mRecognizerManager = new RecognizeManager(this, BD_APIKEY, BD_SECRET);
mTtsManager = new TTSManager(this, BD_APIKEY, BD_SECRET);
mRecognizerManager.setVoiceRecognizeListener(myVoiceRecognizeListener);
mTtsManager.setTTSListener(myTTSListener);
mTuringManager = new TuringManager(this, TURING_APIKEY,
TURING_SECRET);
mTuringManager.setHttpRequestListener(myHttpConnectionListener);
mTtsManager.startTTS("你好啊");
}
/**
* 网络请求回调
*/
HttpRequestListener myHttpConnectionListener = new HttpRequestListener() {
@Override
public void onSuccess(String result) {
if (result != null) {
try {
Log.d(TAG, "result" + result);
JSONObject result_obj = new JSONObject(result);
if (result_obj.has("text")) {
Log.d(TAG, result_obj.get("text").toString());
mHandler.obtainMessage(MSG_SPEECH_START,
result_obj.get("text")).sendToTarget();
}
} catch (JSONException e) {
Log.d(TAG, "JSONException:" + e.getMessage());
}
}
}
@Override
public void onFail(int code, String error) {
Log.d(TAG, "onFail code:" + code + "|error:" + error);
mHandler.obtainMessage(MSG_SPEECH_START, "网络慢脑袋不灵了").sendToTarget();
}
};
/**
* 语音识别回调
*/
RecognizeListener myVoiceRecognizeListener = new RecognizeListener() {
@Override
public void onStartRecognize() {
// 仅针对百度回调
}
@Override
public void onRecordStart() {
}
@Override
public void onRecordEnd() {
}
@Override
public void onRecognizeResult(String result) {
Log.d(TAG, "识别结果:" + result);
if (result == null) {
mHandler.sendEmptyMessage(MSG_RECOGNIZE_START);
return;
}
mHandler.obtainMessage(MSG_RECOGNIZE_RESULT, result).sendToTarget();
}
@Override
public void onRecognizeError(String error) {
Log.e(TAG, "识别错误:" + error);
mHandler.sendEmptyMessage(MSG_RECOGNIZE_START);
}
};
/**
* TTS回调
*/
TTSListener myTTSListener = new TTSListener() {
@Override
public void onSpeechStart() {
Log.d(TAG, "onSpeechStart");
}
@Override
public void onSpeechProgressChanged() {
}
@Override
public void onSpeechPause() {
Log.d(TAG, "onSpeechPause");
}
@Override
public void onSpeechFinish() {
Log.d(TAG, "onSpeechFinish");
mHandler.sendEmptyMessage(MSG_RECOGNIZE_START);
}
@Override
public void onSpeechError(int errorCode) {
Log.d(TAG, "onSpeechError:" + errorCode);
mHandler.sendEmptyMessage(MSG_RECOGNIZE_START);
}
@Override
public void onSpeechCancel() {
Log.d(TAG, "TTS Cancle!");
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.turing.demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<!-- SDK必须 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- 语音识别、合成 -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
但是提供的文档里面有两个接口函数如下的定义描述实际上是搞反了
需要注意下
//停止语音识别操作,代表手动触发用户发音结束事件
- (void)stopRecognize;
//取消正在进行的语音识别操作
- (void)cancleRecognize;