前言

随着人工智能时代的来临,像图像识别,语音识别将会越来越普及,像语音识别的功能在手机软件开发也会是必不可少的功能,目前就语音实别来说,国内现在做的比较好的就是科大讯飞。很多搜索引擎,语音助手都有用到。

开发准备

一、注册科大讯飞的开放者账号、创建应用、下载SDK

       注册地址:http://www.xfyun.cn/

二、创建新应用,在创建新应用里输入你要创建的应用的基本信息平台选择Android。我的是编码应用界面,效果是一样的。

android unity 语音识别 安卓开发语音识别_语音识别

 

三、创建后在我的应用里面可以看到APPID

android unity 语音识别 安卓开发语音识别_android unity 语音识别_02

点击添加新服务可以选择你要使用的服,因为我只用后劲了语音听写,所以只增加了一个

android unity 语音识别 安卓开发语音识别_语音识别_03

五、下载SDK

android unity 语音识别 安卓开发语音识别_java_04

android unity 语音识别 安卓开发语音识别_java_05

上面是我们下载完并解压的SDK。

新建项目

一、新建项目

我们新建了一个AIPLATForm的项目,然后将libs下的两个jar包添加到libs目录下, 将同路径下的其它 .so文件(与c进行交互)复制到main路径下新建的 jniLibs(L要大写)目录下(别忘了jar包要add) , 将assert目录拷贝到main目录下 

android unity 语音识别 安卓开发语音识别_安卓_06

然后在build.gradle(app)里添加

android unity 语音识别 安卓开发语音识别_java_07

二、添加权限

在AndroidManifest.xml里面增加权限,如果是Android6.0后需要动态申请权限。

android unity 语音识别 安卓开发语音识别_android_08

三、语音识别的核心代码

private RecognizerListener mRecoListener;
//初始化并开始语音识别
private void InitVoice() {
    // 将“12345678”替换成您申请的 APPID
    SpeechUtility.createUtility(this, SpeechConstant.APPID + "XXXXXXXXX");

    //1.创建SpeechRecognizer对象,第二个参数:本地听写时传InitListener
    mIat = SpeechRecognizer.createRecognizer(this, null);
    //2.设置听写参数,详见《科大讯飞MSC API手册(Android)》SpeechConstant类
    mIat.setParameter(SpeechConstant.DOMAIN, "iat");
    mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
    mIat.setParameter(SpeechConstant.ACCENT, "mandarin ");

    mRecoListener = new RecognizerListener() {

        @Override
        public void onVolumeChanged(int i, byte[] bytes) {
            //自已写的一个SurFaceView控件,可以不用,就是在判断音量不同的时候有一个动画效果
            surfaceView.DrawVoice(i);
        }

        @Override
        public void onBeginOfSpeech() {

        }

        @Override
        public void onEndOfSpeech() {

        }

        @Override
        public void onResult(RecognizerResult recognizerResult, boolean isLast) {
            if (!isLast) {
                result = parseVoice(recognizerResult.getResultString());
            } else {
                Intent rtnintent = new Intent();
                rtnintent.putExtra("voice", result);
                IatActivity.this.setResult(1, rtnintent);
                IatActivity.this.finish();
            }
        }

        @Override
        public void onError(SpeechError speechError) {
            Intent rtnintent = new Intent();
            rtnintent.putExtra("voice", "");
            IatActivity.this.setResult(-1, rtnintent);
            IatActivity.this.finish();
        }

        @Override
        public void onEvent(int i, int i1, int i2, Bundle bundle) {

        }
    };

    mIat.startListening(mRecoListener);
}

返回的数据是一个json的字符串,所以我们定义了一下类用于解析的

/**
 * 解析语音json
 */
public String parseVoice(String resultString) {
    Gson gson = new Gson();
    Voice voiceBean = gson.fromJson(resultString, Voice.class);

    StringBuffer sb = new StringBuffer();
    ArrayList<Voice.WSBean> ws = voiceBean.ws;
    for (Voice.WSBean wsBean : ws) {
        String word = wsBean.cw.get(0).w;
        sb.append(word);
    }
    return sb.toString();
}

/**
 * 语音对象封装
 */
public class Voice {

    public ArrayList<WSBean> ws;

    public class WSBean {
        public ArrayList<CWBean> cw;
    }

    public class CWBean {
        public String w;
    }
}

下面这个是语音识别这个类的源码:

链接:https://pan.baidu.com/s/1x_EuLPmIiU10kKBwxl0vHg 密码:p57h

下面的编码完的APP的效果视频,自己做的动画有点LOW,随便看看就好