This article has participated in the third phase of the Diggings Creators Camp. For more details, check out diggings program | Creators Camp. The third phase of the diggings Creators Camp is currently underway.

  • 📢 Welcome to like 👍 collect ⭐ message 📝 if there is any error please correct!
  • 📢 this article is written by silly knock code of small Y original 🙉
  • 📢 The future is long and worth all we can for a better life ✨

📢 preface

  • Did anyone leave a comment on an article I wrote about how to access Voice recognition on Flytek
  • He said that there were many parts of the article details are not in place, resulting in that he could not understand
  • So I wrote a three-part series on how to access FlyVoice from a beginner’s perspective! There is also a link at the end of this article!

Note: The version of Unity used in this tutorial is April 32, 2018. Other versions may differ


🍉 Access Baidu voice recognition

  • With the previous article written to access the voice recognition of Exifi, you have to go to the official website to download the corresponding SDK
  • So we went to the website and downloaded the SDK, and we were all smart people

Baidu voice SDK to obtain the url

  • How to get the SDK part without too much introduction, enter the official website according to the introduction
  • Just sign up to get the SDK for voice recognition and create an app

So you’ve created your application, remember the APPID, we’ll use it later!Find the SDK and download one for speech recognition

The next part is to get the speech recognition SDK!


🍓 On Android Studio

1. The project starts

As usual, create a new project, change the name and path, and get started!

Then create a new module and name it file-new-new Module.


2. Access Unity’s classes.jar package

  • Connect the Unity class to the Unity client installation directory
  • Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes
  • Copy the Unity JAR package to the AS libs directory, AS shown below:


3. Access the Flytek Voice classess.jar package

  • The same method, Baidu voice jar package also put in
  • Ar package is located in the core->libs path after extracting the SDK downloaded from the official website
  • Also directly copy to the AS libs directory!

The effect is as follows:


4. Associate two classes.jar packages

  • Select two jar packages, right-click Add As Libray…
  • Just wait for it to compile

  • You can also right-click iFlyteVoice and Open Module Settings
  • Add the.JAR file manually, and then click Apply

If you click on it and it says you already have these two JARS, it means you’re connected


5. Add libmsc. So

Again, find the jniLibs folder under the SDK directory that we downloaded

  • Then copy this folder directly into the SRC ->main directory of AS

As shown in the figure below:


6. Write the SDK interface

All right, so this is where the code comes in, so the first few steps are just to set up a platform for the final operation.

  • Our new next four classes, respectively is CientBaiDuVoiceMainActivity, RecognHandler, RecognListener and GetActivity
  • I’m not going to say how to create them, but I’m going to create two folders Recogn and Util just to make sure that they’re different types of scripts, so just do what I did, okay

Take a look directly at the end result:Then directly on the code of each script, directly into the lineCientBaiDuVoiceMainActivity:

package com.example.baidu;


import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.example.baidu.Recogn.RecognHandler;
import com.example.baidu.Recogn.RecognListener;
import com.unity3d.player.UnityPlayerActivity;

public class CientBaiDuVoiceMainActivity   {

    public static CientBaiDuVoiceMainActivity _instance;

    public static CientBaiDuVoiceMainActivity getInstance(a) {
        if (_instance == null) {
            _instance = new CientBaiDuVoiceMainActivity();
        }
        return _instance;
    }
    // Speech recognition
    RecognHandler mRecognHandler;

    // Speech recognition initialization
    public void InitRecogn(Context context) {
        Log.i("@ @ @"."Android is initiating speech recognition.");
        RecognListener listener=new RecognListener();
        mRecognHandler=new RecognHandler(context,listener);
    }
    // Start speech recognition
    public void StartRecogn(a) {
        mRecognHandler.Start();
    }
    // Stop speech recognition
    public void StopRecogn(a) {
        mRecognHandler.Stop();
    }
    // Release the speech recognition instance
    public void ReleaseRecogn(a) {
        mRecognHandler.Release();
        mRecognHandler=null; }}Copy the code

RecognHandler:

package com.example.baidu.Recogn;// Own package name

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import com.baidu.speech.EventListener;
import com.baidu.speech.EventManager;
import com.baidu.speech.EventManagerFactory;
import com.baidu.speech.asr.SpeechConstant;

import org.json.JSONObject;

import java.util.LinkedHashMap;
import java.util.Map;
import static com.example.baidu.Util.GetActivity.getActivityByContext;

public class RecognHandler {

    private boolean mIsInit = false;
    private EventManager asr;
    private EventListener mEventLisener;

    public RecognHandler(Context context, EventListener listener) {
        if (mIsInit) {
            listener=null;
            return;
        }
        mIsInit = true;
        mEventLisener = listener;
        // Apply for permission dynamically
        initPermission(context);
        asr = EventManagerFactory.create(context, "asr");
        asr.registerListener(listener);
    }
    // Start identification
    public void Start(a) {
        Log.i("@ @ @"."Click on voice recognition.");
        Map<String, Object> params = new LinkedHashMap<String, Object>();
        // Set identification parameters based on SDK integration 2.1
        params.put(SpeechConstant.ACCEPT_AUDIO_VOLUME, true);
        params.put(SpeechConstant.DISABLE_PUNCTUATION, false);
        params.put(SpeechConstant.ACCEPT_AUDIO_DATA, false);
        params.put(SpeechConstant.PID, 1537); // Chinese input model, with commas
        String json = null; // You can replace it with your own JSON
        json = new JSONObject(params).toString(); // This can be replaced with the JSON you want to test
        asr.send(SpeechConstant.ASR_START, json, null.0.0);
    }
    // Stop recognition
    public void Stop(a) {
        asr.send(SpeechConstant.ASR_STOP, null.null.0.0);
    }
    // Release the instance
    public void Release(a) {
        asr.unregisterListener(mEventLisener);
        mIsInit = false;
        asr=null;
    }

    /** * Android 6.0 + requires dynamic permissions */
    private void initPermission(Context context) {
        String permissions[] = {Manifest.permission.RECORD_AUDIO,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.INTERNET,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
        };

        PackageManager pm = getActivityByContext(context).getPackageManager();
        boolean permission_readStorage = (PackageManager.PERMISSION_GRANTED ==
                pm.checkPermission("android.permission.RECORD_AUDIO"."com.cientx.tianguo"));
        boolean permission_network_state = (PackageManager.PERMISSION_GRANTED ==
                pm.checkPermission("android.permission.ACCESS_NETWORK_STATE"."com.cientx.tianguo"));
        boolean permission_internet = (PackageManager.PERMISSION_GRANTED ==
                pm.checkPermission("android.permission.INTERNET"."com.cientx.tianguo"));
        boolean permission_writeStorage = (PackageManager.PERMISSION_GRANTED ==
                pm.checkPermission("android.permission.WRITE_EXTERNAL_STORAGE"."com.cientx.tianguo"));

        if(! (permission_readStorage && permission_writeStorage && permission_network_state && permission_internet)) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                getActivityByContext(context).requestPermissions(permissions, 0x01); }}}}Copy the code

RecognListener:

package com.example.baidu.Recogn;// Fill in your own package name

import android.util.Log;

import com.baidu.speech.EventListener;
import com.baidu.speech.asr.SpeechConstant;
import com.unity3d.player.UnityPlayer;

public class RecognListener implements EventListener {


    @Override
    public void onEvent(String name, String params, byte[] data, int i, int i1) {
        if (name.equals(SpeechConstant.CALLBACK_EVENT_ASR_PARTIAL)) {
            Log.i("@ @ @"."Enter the method to initiate speech recognition.");
            // Identify related results here
            if (params == null || params.isEmpty()) {
                return;
            }
            if (params.contains("\"partial_result\"")) {
                UnityPlayer.UnitySendMessage("NetLogic"."RecognResult", name + "&" + params);
                // The provisional identification result of a sentence
            } else if (params.contains("\"final_result\"")) {
                UnityPlayer.UnitySendMessage("NetLogic"."RecognResult", name + "&" + params);
                // The final recognition result of a sentence
            } else {
                // Not normally run here
                if(data ! =null) {}}}else {
            // Identify start, end, volume, and audio data callbacks
            if(params ! =null && !params.isEmpty()) {
            }
            if(data ! =null) {}}}};Copy the code

GetActivity:

package com.example.baidu.Util;// Own package name

        import android.app.Activity;
        import android.content.Context;
        import android.content.ContextWrapper;

public class GetActivity {


    public static Activity getActivityByContext(Context context){
        while(context instanceof ContextWrapper){
            if(context instanceof Activity){
                return (Activity) context;
            }
            context = ((ContextWrapper) context).getBaseContext();
        }
        return null; }}Copy the code

These four scripts are the core class, speech recognition is AS end of these scripts to achieve! Let’s take a look at how to modify AndroidManifest!


7. Modify the AndroidManifest file

Just copy my code into your own AndroidManifest

  • There are two caveats here
  • Package to fill in the correct, fill in their own!
  • The following APPID, API_KEY and SECRET_KEY should be filled in the application you created on Baidu’s speech recognition platform
  • If you forget to look up oh, this to baidu voice platform to see their own! Everyone’s is different
<? xml version="1.0" encoding="utf-8"? > <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.xxx.xxx"> <! -- Fill in your own package name --> <! <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/ > <! -- voice recognition permission --> <uses-permission Android :name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/ > <! <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS"
        tools:ignore="ProtectedPermissions" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
 
    <application android:allowBackup="true">
        <activity android:name=".Wakeup.WakeUpResult"></activity>
        <activity android:name=".Wakeup.WakeupHandler" />
        
        <activity
            android:name=".CientBaiDuVoiceMainActivity"
            android:launchMode="singleTask"/ > <! -- Please fill in the real APP_ID API_KEY SECRET_KEY --> <meta-data android:name="com.baidu.speech.APP_ID"
            android:value="xxxxx" />
        <meta-data
            android:name="com.baidu.speech.API_KEY"
            android:value="xxxxx" />
        <meta-data
            android:name="com.baidu.speech.SECRET_KEY"
            android:value="xxxxx" />
 
        <service
            android:name="com.baidu.speech.VoiceRecognitionService"
            android:exported="false" />
    </application>
 
</manifest>
Copy the code

8. Packaging aar

  • Ok, if you make sure the package name, ID, KEY, etc. in the previous step are filled in correctly
  • Then the operation on the AS side is complete, and the next step is to package the AAR for Unity to use.
  • As shown in the following figure, select the baidu folder and then Build – > Make Module “baidu”
  • Before starting these three cloth, click the small button on the right to compile the best!

  • Then you will see a Build folder under the Baidu folder!
  • Let’s copy the AAR file from the image below and use it in Unity next!!


🍑 Procedure in Unity

1. Create a Unity project

Open UnityHub and create a New Unity project. The Unity version I used here is April 32, 2018


2. Import the AAR package

  • Create a new folder under Unity’s Assets folder Plugins->Android
  • Don’t ask why, just do it ~
  • Then put the AAR file we packaged on the AS side into Unity, AS shown below:


3. Simply build a UI for testing

Create a new canvas and put two buttons and a Text in it!

3. Create a new script to write code!

  • Here we create an empty gameobject and change the name to NetLogic!
  • Name must be that, because this time AS we give Unity interaction is message mechanism UnityPlayer. UnitySendMessage
  • The object is found by name! (Because I wrote the Voice in the use of the Proxy mode, so here a different way to experience!)

  • Then create a new script BD, the code is as follows, drag two Button and Text Text onto the script!

The diagram below:

using LitJson;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BD : MonoBehaviour
{
    AndroidJavaObject m_AndroidPluginObj;
    AndroidJavaClass _androidJC;
    AndroidJavaObject m_Android;
    public Text mRecognRes;
    public Button startASR_Btn;
    public Button stopASR_Btn;

    void Start()
    {
        AndroidJavaClass _androidJC = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        if (_androidJC == null)
        {
            Debug.Log("JNI initialization failure.");
            return;
        }
        m_AndroidPluginObj = _androidJC.GetStatic<AndroidJavaObject>("currentActivity");
        startASR_Btn.onClick.AddListener(StartRecogn);
        stopASR_Btn.onClick.AddListener(StopRecogn);

        Invoke("InitAsr".3);
    }

    public void InitAsr()
    {
        AndroidJavaClass jc = new AndroidJavaClass("com.example.baidu.CientBaiDuVoiceMainActivity");// Package name plus class name
        AndroidJavaObject m_Android = jc.CallStatic<AndroidJavaObject>("getInstance");
        if(m_Android ! =null)
        {
            m_Android.Call("InitRecogn", m_AndroidPluginObj);
        }
        else
            Debug.Log("AndroidPlugin is Null");
    }
    public void StartRecogn()
    {
        AndroidJavaClass jc = new AndroidJavaClass("com.example.baidu.CientBaiDuVoiceMainActivity");
        AndroidJavaObject m_Android = jc.CallStatic<AndroidJavaObject>("getInstance");
        if(m_Android ! =null)
        {
            m_Android.Call("StartRecogn");
        }
        else
            Debug.Log("AndroidPlugin is Null");
    }
    public void StopRecogn()
    {
        AndroidJavaClass jc = new AndroidJavaClass("com.example.baidu.CientBaiDuVoiceMainActivity");
        AndroidJavaObject m_Android = jc.CallStatic<AndroidJavaObject>("getInstance");
        if(m_Android ! =null)
        {
            m_Android.Call("StopRecogn");
        }
        else
            Debug.Log("AndroidPlugin is Null");
    }

    /// <summary>
    ///Baidu voice recognition results feedback
    /// </summary>
    /// <param name="res"></param>
    void RecognResult(string res)
    {
        string[] ress = res.Split('&');
        JsonData jsonData = JsonMapper.ToObject(ress[1]);
        string resStr = "";
        if (jsonData["result_type"].ToString() == "partial_result")
        {
            resStr = "Provisional identification results :";
        }
        else
        {
            resStr = Final identification result:;
        }

        resStr += jsonData["best_result"].ToString(); mRecognRes.text = resStr; }}Copy the code
  • The script defines two Button click events and four methods

  • Are speech recognition initialization, start speech recognition, stop speech recognition and speech recognition content reception!

  • It is worth mentioning here that the method of receiving speech recognition content is Json characters

  • We need to parse to see what we want

  • Here you need to import a DLL file for parsing Json — LitJSON

  • And put it in your Plugins directory

  • That I here is also on the Internet to download and then put here to use!

  • Then you need to reference the namespace in the code, which is also added in the script code, here just briefly!
using LitJson;
Copy the code

4. Package into APK real machine test

  • Just change the product name to the Package name in AndroidManifest
  • For example, the package name here can be changed to Baidu, and then Build package is ok
  • My own true machine has tested, can recognize!


🥭 source code project download

  • The source code project for this tutorial can be downloaded here
  • Can be downloaded directly open to use

Unity access Baidu voice source project download


💬 summary

  • That ends here, this article made a tutorial on Baidu voice access

Compared to the first three articles that access Flyvoice, there is less to write, because the finishing process is somewhat similar!

  • In the actual operation process, it is certain that there will be some problems
  • Including when I was doing this Baidu speech recognition also encountered difficulties
  • Because I have done the voice recognition of Flytek before, so I feel baidu should be very simple, the truth should be similar
  • But I actually operate, including their own code and write article summary found that the difference between the two is quite big!
  • Anyway, the difficulties will be solved, more than a few times to find where the problem
  • This process is necessary for programmers, but also the most brain pain problem, refueling!