前言:
此demo阉割了离线功能,只支持在线识别,并删除了根据代码报错参数纠错的部分,整个部分自己写的代码也就六七十行左右,显得非常简洁明了,很多部分都写了备注,希望大家能够获得些帮助。
准备工作
- 首先去百度AI开放平台注册账号,找到产品服务找到语音识别,根据文档申请API密钥。
- 然后去2.然后去下载SDK,解压包,打开readme_README_IMPORTANT.txt,阅读,找到库文件路径:
jar :core/libs 目录下
so:core/src/main/jniLibs 目录
3.把jar包导入apps/libs,右击选择add as library;
把jniLibs包里的东西导到自己工程包里(main里新建jniLibs)。
搭建环境
权限申请:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<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.WRITE_EXTERNAL_STORAGE" />
API、密钥:
<meta-data
android:name="com.baidu.speech.APP_ID"
android:value="替换成你申请的AppID"/>
<meta-data
android:name="com.baidu.speech.API_KEY"
android:value="替换成你申请的API Key" />
<meta-data
android:name="com.baidu.speech.SECRET_KEY"
android:value="替换成你申请的Secret Key" />
服务
<service android:name="com.baidu.speech.VoiceRecognitionService" android:exported="false" />
代码部分:
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/txtResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:textSize="18dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#999" />
<TextView
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:textSize="18dp" />
<Button
android:layout_gravity="end"
android:id="@+id/btn_stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="停止" />
<Button
android:id="@+id/btn"
android:layout_gravity="end"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="开始" />
</LinearLayout>
Java:
public class MainActivity extends AppCompatActivity implements com.baidu.speech.EventListener {
protected TextView txtResult;
protected Button btn;
protected Button stopBtn;
private EventManager asr;
private void start(){
Map<String,Object> params = new LinkedHashMap<>();//传递Map<String,Object>的参数,会将Map自动序列化为json
String event = null;
event = SpeechConstant.ASR_START;
params.put(SpeechConstant.ACCEPT_AUDIO_VOLUME,false);//回调当前音量
String json = null;
json = new JSONObject(params).toString();//demo用json数据来做数据交换的方式
asr.send(event, json, null, 0, 0);// 初始化EventManager对象,这个实例只能创建一次,就是我们上方创建的asr,此处开始传入
}
private void stop(){
txtResult.append("停止识别:ASR_STOP");
asr.send(SpeechConstant.ASR_STOP, null, null, 0, 0);//此处停止
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initPermission();
asr = EventManagerFactory.create(this,"asr");//注册自己的输出事件类
asr.registerListener(this); 调用 EventListener 中 onEvent方法
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
start();
}
});
stopBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stop();
}
});
}
@Override
protected void onPause() {
super.onPause();
asr.send(SpeechConstant.ASR_CANCEL, "{}", null, 0, 0);
}
@Override
protected void onDestroy() {
super.onDestroy();
asr.send(SpeechConstant.ASR_CANCEL, "{}", null, 0, 0);
asr.unregisterListener(this);//退出事件管理器
// 必须与registerListener成对出现,否则可能造成内存泄露
}
public void onEvent(String name, String params, byte[] data, int offset, int length) {
String resultTxt = null;
if (name.equals(SpeechConstant.CALLBACK_EVENT_ASR_PARTIAL)){//识别结果参数
if (params.contains("\"final_result\"")){//语义结果值
try {
JSONObject json = new JSONObject(params);
String result = json.getString("best_result");//取得key的识别结果
resultTxt = result;
} catch (JSONException e) {
e.printStackTrace();
}
}
}
if (resultTxt != null){
resultTxt += "\n";
txtResult.append(resultTxt);
}
}
private void initView() {
txtResult = findViewById(R.id.txtResult);
btn = findViewById(R.id.btn);
stopBtn = findViewById(R.id.btn_stop);
}
private void initPermission() {
String permissions[] = {Manifest.permission.RECORD_AUDIO,
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.INTERNET,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
ArrayList<String> toApplyList = new ArrayList<String>();
for (String perm :permissions){
if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, perm)) {
toApplyList.add(perm);
//进入到这里代表没有权限.
}
}
String tmpList[] = new String[toApplyList.size()];
if (!toApplyList.isEmpty()){
ActivityCompat.requestPermissions(this, toApplyList.toArray(tmpList), 123);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
// 此处为android 6.0以上动态授权的回调,用户自行实现。
}
}
Android P用户注意:
Android P为了加强安全性禁止了所有的未加密连接,默认屏蔽所有的HTTP,解决方法:
android:usesCleartextTraffic="true"
文档:http://ai.baidu.com/docs#/ASR-Android-SDK/top
SDK下载:https://ai.baidu.com/sdk#asr
github:https://github.com/xuguilin0802/BaiduSpeech