百度语音识别Demo -- 简单应用

先到百度语音官网上查看集成指南
这里写图片描述

然后再去下载语音识别-离在线融合SDK(bd_speech_sdk_asr_v3.0.3.20170801_60da871.zip)
这里写图片描述

直接参考SDK中的ActivityMiniRecog类


这里,我就直接上代码了,我是在Android Studio 2.3.3 平台上编译的
1、app\build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "com.sky.baiduspeechdemo"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    //compile fileTree(dir: 'libs', include: ['*.jar'])
    compile fileTree(exclude: '*.bak', dir: 'libs')
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.google.code.gson:gson:2.8.2'
}

2、AndroidManifest.xml


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sky.baiduspeechdemo">

    
    <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" />
    

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        
        <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" />

        <activity android:name=".MainActivity"
            android:configChanges="orientation|keyboardHidden|screenLayout"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            intent-filter>
        activity>
    application>

manifest>

这里写图片描述

3、将SDK中的app/libs/bdasr_V3_20170801_60da871.jar 复制到您的项目的同名目录中。
4、将SDK中的app/src/main/jniLibs 下armeabi等5个目录,复制到您的项目的同名目录中。
这里写图片描述


5、activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.sky.baiduspeechdemo.MainActivity">

    <Button
        android:id="@+id/btnStartRecord"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/start_record"/>
    <Button
        android:id="@+id/btnStopRecord"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/stop_record"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:orientation="vertical">
        <View
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:background="#000"/>
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/tvResult"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/parse_process" />
        ScrollView>
    LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">
        <View
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:background="#000"/>
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/tvParseResult"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/parse_result" />
        ScrollView>
    LinearLayout>
LinearLayout>

6、MainActivity.java

package com.sky.baiduspeechdemo;

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.baidu.speech.EventListener;
import com.baidu.speech.EventManager;
import com.baidu.speech.EventManagerFactory;
import com.baidu.speech.asr.SpeechConstant;
import com.google.gson.Gson;
import com.sky.baiduspeechdemo.asrfinishjson.AsrFinishJsonData;
import com.sky.baiduspeechdemo.asrpartialjson.AsrPartialJsonData;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements EventListener {

    private static final String TAG = "MainActivity";

    private Button btnStartRecord;
    private Button btnStopRecord;
    private TextView tvResult;
    private TextView tvParseResult;

    private EventManager asr;

    private boolean logTime = true;

    private String final_result;

    @Override
    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方法

        btnStartRecord.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                start();
            }
        });
        btnStopRecord.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                stop();
            }
        });
    }

    private void initView() {
        tvResult = (TextView) findViewById(R.id.tvResult);
        tvParseResult = (TextView) findViewById(R.id.tvParseResult);
        btnStartRecord = (Button) findViewById(R.id.btnStartRecord);
        btnStopRecord = (Button) findViewById(R.id.btnStopRecord);
        btnStopRecord.setVisibility(View.GONE);
    }

    /**
     * android 6.0 以上需要动态申请权限
     */
    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 toApplyList = new ArrayList();

        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以上动态授权的回调,用户自行实现。
    }

    /*
    * EventListener  回调方法
    * name:回调事件
    * params: JSON数据,其格式如下:
    *
    * */
    @Override
    public void onEvent(String name, String params, byte[] data, int offset, int length) {
        String result = "";

        if (length > 0 && data.length > 0) {
            result += ", 语义解析结果:" + new String(data, offset, length);
        }

        if (name.equals(SpeechConstant.CALLBACK_EVENT_ASR_READY)) {
            // 引擎准备就绪,可以开始说话
            result += "引擎准备就绪,可以开始说话";

        } else if (name.equals(SpeechConstant.CALLBACK_EVENT_ASR_BEGIN)) {
            // 检测到用户的已经开始说话
            result += "检测到用户的已经开始说话";

        } else if (name.equals(SpeechConstant.CALLBACK_EVENT_ASR_END)) {
            // 检测到用户的已经停止说话
            result += "检测到用户的已经停止说话";
            if (params != null && !params.isEmpty()) {
                result += "params :" + params + "\n";
            }
        } else if (name.equals(SpeechConstant.CALLBACK_EVENT_ASR_PARTIAL)) {
            // 临时识别结果, 长语音模式需要从此消息中取出结果
            result += "识别临时识别结果";
            if (params != null && !params.isEmpty()) {
                result += "params :" + params + "\n";
            }
//            Log.d(TAG, "Temp Params:"+params);
            parseAsrPartialJsonData(params);
        } else if (name.equals(SpeechConstant.CALLBACK_EVENT_ASR_FINISH)) {
            // 识别结束, 最终识别结果或可能的错误
            result += "识别结束";
            btnStartRecord.setEnabled(true);
            asr.send(SpeechConstant.ASR_STOP, null, null, 0, 0);
            if (params != null && !params.isEmpty()) {
                result += "params :" + params + "\n";
            }
            Log.d(TAG, "Result Params:"+params);
            parseAsrFinishJsonData(params);
        }
        printResult(result);
    }

    private void printResult(String text) {
        tvResult.append(text + "\n");
    }

    private void start() {
        tvResult.setText("");
        btnStartRecord.setEnabled(false);
        Map params = new LinkedHashMap();
        String event = null;
        event = SpeechConstant.ASR_START;
        params.put(SpeechConstant.PID, 1536); // 默认1536
        params.put(SpeechConstant.DECODER, 0); // 纯在线(默认)
        params.put(SpeechConstant.VAD, SpeechConstant.VAD_DNN); // 语音活动检测
        params.put(SpeechConstant.VAD_ENDPOINT_TIMEOUT, 2000); // 不开启长语音。开启VAD尾点检测,即静音判断的毫秒数。建议设置800ms-3000ms
        params.put(SpeechConstant.ACCEPT_AUDIO_DATA, false);// 是否需要语音音频数据回调
        params.put(SpeechConstant.ACCEPT_AUDIO_VOLUME, false);// 是否需要语音音量数据回调

        String json = null; //可以替换成自己的json
        json = new JSONObject(params).toString(); // 这里可以替换成你需要测试的json
        asr.send(event, json, null, 0, 0);
        printResult("输入参数:" + json);
    }

    private void stop() {
        asr.send(SpeechConstant.ASR_STOP, null, null, 0, 0);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        asr.send(SpeechConstant.ASR_CANCEL, "{}", null, 0, 0);
    }

    private void parseAsrPartialJsonData(String data) {
        Log.d(TAG, "parseAsrPartialJsonData data:"+data);
        Gson gson = new Gson();
        AsrPartialJsonData jsonData = gson.fromJson(data, AsrPartialJsonData.class);
        String resultType = jsonData.getResult_type();
        Log.d(TAG, "resultType:"+resultType);
        if(resultType != null && resultType.equals("final_result")){
            final_result = jsonData.getBest_result();
//            tvParseResult.setText("解析结果:" + final_result);
        }
    }

    private void parseAsrFinishJsonData(String data) {
        Log.d(TAG, "parseAsrFinishJsonData data:"+data);
        Gson gson = new Gson();
        AsrFinishJsonData jsonData = gson.fromJson(data, AsrFinishJsonData.class);
        String desc = jsonData.getDesc();
        if(desc !=null && desc.equals("Speech Recognize success.")){
            tvParseResult.setText("解析结果:" + final_result);
        }else{
            String errorCode = "\n错误码:" + jsonData.getError();
            String errorSubCode = "\n错误子码:"+ jsonData.getSub_error();
            String errorResult = errorCode + errorSubCode;
            tvParseResult.setText("解析错误,原因是:" + desc + "\n" + errorResult);
        }
    }
}

7、asrfinishjson/AsrFinishJsonData.java

package com.sky.baiduspeechdemo.asrfinishjson;

public class AsrFinishJsonData {
    private String error;
    private String sub_error;
    private String desc;
    private OriginResult origin_result;

    public String getError() {
        return error;
    }

    public String getSub_error() {
        return sub_error;
    }

    public String getDesc() {
        return desc;
    }

    public OriginResult getOrigin_result() {
        return origin_result;
    }
}

8、asrfinishjson\OriginResult.java

package com.sky.baiduspeechdemo.asrfinishjson;

public class OriginResult {
    private String error;
    private String sub_error;
    private String sn;
    private String desc;

    public String getSn() {
        return sn;
    }

    public String getDesc() {
        return desc;
    }

    public String getError() {
        return error;
    }

    public String getSub_error() {
        return sub_error;
    }
}

9、asrpartialjson\AsrPartialJsonData.java

package com.sky.baiduspeechdemo.asrpartialjson;
import java.util.ArrayList;

public class AsrPartialJsonData {
    private ArrayList results_recognition;
    private OriginResult origin_result;
    private String error;
    private String best_result;
    private String result_type;

    public ArrayList getResults_recognition() {
        return results_recognition;
    }

    public OriginResult getOrigin_result() {
        return origin_result;
    }

    public String getBest_result() {
        return best_result;
    }

    public String getError() {
        return error;
    }

    public String getResult_type() {
        return result_type;
    }
}

10、asrpartialjson\OriginResult.java

package com.sky.baiduspeechdemo.asrpartialjson;

public class OriginResult {

    private String corpus_no;
    private String err_no;
    private String sn;
    private Result result;

    public Result getResult() {
        return result;
    }

    public String getCorpus_no() {
        return corpus_no;
    }

    public String getErr_no() {
        return err_no;
    }

    public String getSn() {
        return sn;
    }
}

11、asrpartialjson\Result.java

package com.sky.baiduspeechdemo.asrpartialjson;

import java.util.ArrayList;
import java.util.List;

public class Result {
    private ArrayList uncertain_word;
    private ArrayList word;

    public ArrayList getUncertain_word() {
        return uncertain_word;
    }

    public ArrayList getWord() {
        return word;
    }
}

工程文件如下:
这里写图片描述

你可能感兴趣的:(Android)