AS项目集成科大讯飞语音识别功能
关于如何注册、下载SDK Demo的操作,请参考其他高手的文档,本文集中精力在如何将语音识别功能集成到AS项目。1、将SpeechDemo下的Msc.jar包拷贝到新建项目的libs下2、将SpeechDemo下的arm文件夹拷贝到新建项目main下新建项目下面的main新建Jnilibs文件夹,将两个arm文件拷贝进去3、将SpeechDemo main下的assets文件夹拷贝到新建项目mai
·
关于如何注册、下载SDK Demo的操作,请参考其他高手的文档,本文集中精力在如何将语音识别功能集成到AS项目。
1、将SpeechDemo下的Msc.jar包拷贝到新建项目的libs下
2、将SpeechDemo下的arm文件夹拷贝到新建项目main下
新建项目下面的main新建Jnilibs文件夹,将两个arm文件拷贝进去
3、将SpeechDemo main下的assets文件夹拷贝到新建项目main下
assets包含的是一些运行时的动态效果
4、修改gradle文件
增加如下语句:
5、书写activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="130dp"
android:layout_marginRight="130dp"
android:layout_marginTop="30dp"
android:text="按住说话"
/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"/>
</LinearLayout>
6、书写MainActivity.java文件
package com.example.administrator.testlistener;
import java.util.HashMap;
import java.util.LinkedHashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.ui.RecognizerDialog;
import com.iflytek.cloud.ui.RecognizerDialogListener;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
//存放听写分析结果文本
private HashMap<String, String> hashMapTexts = new LinkedHashMap<>();
private Button btn; //初始化控件
private TextView txt;
SpeechRecognizer hearer; //听写对象
RecognizerDialog dialog; //讯飞提示框
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 语音配置对象初始化
SpeechUtility.createUtility(MainActivity.this, SpeechConstant.APPID + "=xxxxxx【注:替换成你的appid】");
btn = findViewById(R.id.btn);
txt = findViewById(R.id.txt);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 1.创建SpeechRecognizer对象,第2个参数:本地听写时传InitListener
hearer = SpeechRecognizer.createRecognizer( MainActivity.this, null);
// 交互动画
dialog = new RecognizerDialog(MainActivity.this, null);
// 2.设置听写参数,详见《科大讯飞MSC API手册(Android)》SpeechConstant类
hearer.setParameter(SpeechConstant.DOMAIN, "iat"); // domain:域名
hearer.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
hearer.setParameter(SpeechConstant.ACCENT, "mandarin"); // mandarin:普通话
//3.开始听写
dialog.setListener(new RecognizerDialogListener() { //设置对话框
@Override
public void onResult(RecognizerResult results, boolean isLast) {
// TODO 自动生成的方法存根
Log.d("Result", results.getResultString());
//(1) 解析 json 数据<< 一个一个分析文本 >>
StringBuffer strBuffer = new StringBuffer();
try {
JSONTokener tokener = new JSONTokener(results.getResultString());
Log.i("TAG", "Test"+results.getResultString());
Log.i("TAG", "Test"+results.toString());
JSONObject joResult = new JSONObject(tokener);
JSONArray words = joResult.getJSONArray("ws");
for (int i = 0; i < words.length(); i++) {
// 转写结果词,默认使用第一个结果
JSONArray items = words.getJSONObject(i).getJSONArray("cw");
JSONObject obj = items.getJSONObject(0);
strBuffer.append(obj.getString("w"));
}
} catch (Exception e) {
e.printStackTrace();
}
// String text = strBuffer.toString();
// (2)读取json结果中的sn字段
String sn = null;
try {
JSONObject resultJson = new JSONObject(results.getResultString());
sn = resultJson.optString("sn");
} catch (JSONException e) {
e.printStackTrace();
}
//(3) 解析语音文本<< 将文本叠加成语音分析结果 >>
hashMapTexts.put(sn, strBuffer.toString());
StringBuffer resultBuffer = new StringBuffer(); //最后结果
for (String key : hashMapTexts.keySet()) {
resultBuffer.append(hashMapTexts.get(key));
}
txt.setText(resultBuffer.toString());
}
@Override
public void onError(SpeechError error) {
// TODO 自动生成的方法存根
error.getPlainDescription(true);
}
});
dialog.show(); //显示对话框
}
});
}
}
7、效果图
更多推荐
所有评论(0)