前面的工程项目配置和语音识别差不多,但是需要从SDK的res文件夹中复制ivw文件夹粘贴到main下面的assets文件夹下面。具体的文件配置结构,我截个图给大家看看:
剩下的步骤就是两页代码了,附上!
首先是MyApplication中的代码:
public class MyApplication extends Application {@Override
public void onCreate() {initializeIflytek();super.onCreate();
}private void initializeIflytek()
{StringBuffer param = new StringBuffer();//IflytekAPP_id为我们申请的Appidparam.append("appid="+getString(R.string.IflytekAPP_id));param.append(",");// 设置使用v5+param.append(SpeechConstant.ENGINE_MODE+"="+ SpeechConstant.MODE_MSC);SpeechUtility.createUtility(MyApplication.this, param.toString());
}
}
然后是MainActivity中的代码:
package com.example.study01;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.os.Environment;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.VoiceWakeuper;
import com.iflytek.cloud.WakeuperListener;
import com.iflytek.cloud.WakeuperResult;
import com.iflytek.cloud.util.ResourceUtil;import org.json.JSONException;
import org.json.JSONObject;public class MainActivity extends AppCompatActivity {//唤醒的阈值,就相当于门限值,当用户输入的语音的置信度大于这一个值的时候,才被认定为成功唤醒。
private int curThresh = 1450;//是否持续唤醒
private String keep_alive = "1";private String ivwNetMode = "0";
// 语音唤醒对象
private VoiceWakeuper mIvw;
//存储唤醒词的ID
private String wordID = "";
// 唤醒结果内容
private String resultString;private TextView tv;@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv=(TextView)findViewById(R.id.tv);// 初始化唤醒对象mIvw = VoiceWakeuper.createWakeuper(MainActivity.this, null);//非空判断,防止因空指针使程序崩溃mIvw = VoiceWakeuper.getWakeuper();if(mIvw != null) {resultString="";tv.setText(resultString);// 清空参数mIvw.setParameter(SpeechConstant.PARAMS, null);// 唤醒门限值,根据资源携带的唤醒词个数按照“id:门限;id:门限”的格式传入mIvw.setParameter(SpeechConstant.IVW_THRESHOLD, "0:"+ curThresh);// 设置唤醒模式mIvw.setParameter(SpeechConstant.IVW_SST, "wakeup");// 设置持续进行唤醒mIvw.setParameter(SpeechConstant.KEEP_ALIVE, keep_alive);// 设置闭环优化网络模式mIvw.setParameter(SpeechConstant.IVW_NET_MODE, ivwNetMode);// 设置唤醒资源路径mIvw.setParameter(SpeechConstant.IVW_RES_PATH, getResource());// 设置唤醒录音保存路径,保存最近一分钟的音频mIvw.setParameter( SpeechConstant.IVW_AUDIO_PATH, Environment.getExternalStorageDirectory().getPath()+"/msc/ivw.wav" );mIvw.setParameter( SpeechConstant.AUDIO_FORMAT, "wav" );// 如有需要,设置 NOTIFY_RECORD_DATA 以实时通过 onEvent 返回录音音频流字节//mIvw.setParameter( SpeechConstant.NOTIFY_RECORD_DATA, "1" );// 启动唤醒mIvw.startListening(new MyWakeuperListener());}else{showTip("唤醒未初始化");}
}public void showTip(String str){Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
}/*** 获取唤醒词功能* @return 返回文件位置*/
private String getResource() {final String resPath = ResourceUtil.generateResourcePath(MainActivity.this,ResourceUtil.RESOURCE_TYPE.assets, "ivw/"+getString(R.string.IflytekAPP_id)+".jet");return resPath;
}/*** 唤醒词监听类* @author Administrator**/
private class MyWakeuperListener implements WakeuperListener {//开始说话@Overridepublic void onBeginOfSpeech() {}//错误码返回@Overridepublic void onError(SpeechError arg0) {}@Overridepublic void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {}@Overridepublic void onVolumeChanged(int i) {}@Overridepublic void onResult(WakeuperResult result) {if (!"1".equalsIgnoreCase(keep_alive)) {//setRadioEnable(true);}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) {e.printStackTrace();}tv.setText(resultString);}
}
}
最后别忘记添加录音动态权限
大差不差应该就是这些了,但是还是怕同学们会出错,比如:appid是啥没有搞清楚,还有返回文件的位置没有弄清楚,细节吧,我这个是简单的实现语音唤醒功能,希望能对大家有所帮助!