android studio讯飞语音唤醒
时间: 2023-11-20 17:56:34 浏览: 192
讯飞语音唤醒在Android Studio中的实现过程如下:
1.在项目的build.gradle文件中添加以下依赖:
```gradle
implementation 'com.iflytek:speechcloud:1.0.0'
```
2.在AndroidManifest.xml文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
```
3.在需要使用语音唤醒的Activity中,初始化唤醒对象并设置唤醒参数:
```java
// 初始化唤醒对象
private SpeechWakeuper mIvw;
// 唤醒结果内容
private String resultString;
// 唤醒监听器
private WakeuperListener mWakeuperListener = new WakeuperListener() {
@Override
public void onResult(WakeuperResult result) {
try {
String text = result.getResultString();
JSONObject object;
object = new JSONObject(text);
StringBuffer buffer = new StringBuffer();
buffer.append("【RAW】 " + text);
buffer.append("\n");
buffer.append("【操作类型】" + object.optString("sst"));
buffer.append("\n");
buffer.append("【唤醒词id】" + object.optString("id"));
buffer.append("\n");
buffer.append("【得分】" + object.optString("score"));
buffer.append("\n");
buffer.append("【前端点】" + object.optString("bos"));
buffer.append("\n");
buffer.append("【尾端点】" + object.optString("eos"));
resultString = buffer.toString(); } catch (JSONException e) {
resultString = "结果解析出错";
e.printStackTrace();
}
// 显示唤醒结果
mResultText.setText(resultString);
}
@Override
public void onError(SpeechError error) {
// 显示错误信息
mResultText.setText(error.getPlainDescription(true));
}
@Override
public void onBeginOfSpeech() {
}
@Override
public void onEvent(int eventType, int isLastEvent, int arg2, Bundle obj) {
}
@Override
public void onVolumeChanged(int volume) {
}
};
// 初始化唤醒对象
mIvw = SpeechWakeuper.createWakeuper(this, null);
// 设置唤醒参数,详见讯飞官方文档
mIvw.setParameter(SpeechConstant.PARAMS, null);
mIvw.setParameter(SpeechConstant.IVW_THRESHOLD, "0:" + curThresh);
mIvw.setParameter(SpeechConstant.IVW_SST, "wakeup");
mIvw.setParameter(SpeechConstant.IVW_SHOT_WORD, "讯飞语音唤醒");
mIvw.setParameter(SpeechConstant.IVW_ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
mIvw.setParameter(SpeechConstant.IVW_RES_PATH, getResource());
mIvw.setParameter(SpeechConstant.IVW_AUDIO_PATH, getAudioResource());
mIvw.setParameter(SpeechConstant.AUDIO_FORMAT, "wav");
```
4.在需要启动语音唤醒的时候,调用以下方法:
```java
mIvw.startListening(mWakeuperListener);
```
阅读全文