unity access to iFLYTEK speech recognition SDK

1: Download the official SDK and open it with ASThen create a new Module

2: Drag the Classes in the unit and the MSCs in the official Demo to Libs, and import them into the library. Right click to open an add Button.

3: Create a new jniLibs folder under main, and drag armeabi-v7a from official Demo into it

4: Drag the JSON parsing script in the official demo to the package name folder, create a new script, ManiActivity, and open the ManiActivity script

5: Write code

package com.jinchidong.xunfeilibrary;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;

import com.iflytek.cloud.RecognizerListener;
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.jinchidong.xunfeilibrary.JsonParser;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.LinkedHashMap;

public class MainActivity extends UnityPlayerActivity {
    private SpeechRecognizer mIat;
    // Storing dictation results with HashMap
    private HashMap<String, String> mIatResults = new LinkedHashMap<String, String>();

    protected void onCreate(Bundle savedInstanceState) {
        // setContentView(R.layout.activity_main);

        // Initialization engine, APPID application in iFLYTEK console.
        SpeechUtility.createUtility(this, SpeechConstant.APPID + "=5e6ed77c");
        // Create a speech recognition object (under cloud package). Iat is the shorthand for iFLYTEK's speech dictation. The second parameter is the successful or failed listener, which is simply null.
        mIat = SpeechRecognizer.createRecognizer(this, null);
        // Setting parameters
        mIat.setParameter(SpeechConstant.DOMAIN, "iat");      // Service type, voice dictation
        mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");  // The language is Chinese
        mIat.setParameter(SpeechConstant.ACCENT, "mandarin"); // The accent is Mandarin
        mIat.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); // Engine type, cloud engine

    // Event listener
    private RecognizerListener mRecognizer = new RecognizerListener() {
        public void onVolumeChanged(int i, byte[] bytes) {
			//Called when the sound changes

        public void onBeginOfSpeech() {
			//Call when you start talking

        public void onEndOfSpeech() {
			//Call at the end of the conversation

        public void onResult(RecognizerResult recognizerResult, boolean b) {
            //Return text results

        public void onError(SpeechError speechError) {
            //Called when an error is reported

        public void onEvent(int i, int i1, int i2, Bundle bundle) {
			//Custom events

   //unity calls this method to start recording resolution
    public void startListen(){
	//This is the android pasteboard reference I need is irrelevant to this article
    public void  StrCopy(String s){
            //Get clipboard manager:
            ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            // Create normal character ClipData
            ClipData clipData = ClipData.newPlainText(null, s);
            // Place the ClipData contents on the system clipboard.
	//False quote
    public void printError(SpeechError s){
        UnityPlayer.UnitySendMessage("XunfeiManager", "onError", s.toString());

     * Print out the results. This method is copied from latDemo.
     * JsonParser The tool class is copied from speechDemo/java/com.iflytek/speech/util/JsonParser.java to xunfiilibrary / Java / package name /.
     * @param results
    private void printResult(RecognizerResult results) {
        String text = JsonParser.parseIatResult(results.getResultString());

        String sn = null;
        // Read sn field in json result
        try {
            JSONObject resultJson = new JSONObject(results.getResultString());
            sn = resultJson.optString("sn");
        } catch (JSONException e) {

        mIatResults.put(sn, text);

        StringBuffer resultBuffer = new StringBuffer();
        for (String key : mIatResults.keySet()) {

         * Show Unity the final results. Android calls Unity
         * Parameter 1: GameObject name in Unity scene.
         * Parameter 2: the specified method in any script of the object body.
         * Parameter 3: call the parameter of the method.
        UnityPlayer.UnitySendMessage("XunfeiManager", "OnResult", resultBuffer.toString());


6: Open the Android manifest.xml script in the Module we created, and then open the app Copy the Android manifest.xml of the Module, replace the Application tag in the Android manifest.xml script created by us, open the Android manifest.xml script in the official Demo, and paste the permissions into our script. The final code should be as follows

   	<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            <meta-data android:name="unityplayer.UnityActivity" android:value="true"/>

    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


7: Finally, you can type the AAR package. If you encounter any errors reported by the official Demo, please delete the official Demo directly

8: After opening the output folder, we found that the aar package opened it and deleted the Classes in libs. Import into unity ---- plugins Android

9: Write end unit code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Main : MonoBehaviour
    public Text errorText;
    public Text resultText;
    private AndroidJavaObject currentActivity;

    private void Start()

        AndroidJavaClass javaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        currentActivity = javaClass.GetStatic<AndroidJavaObject>("currentActivity");

    /// <summary>
    ///Click the button to start voice input
    /// </summary>
    public void OnClick()

    /// <summary>
    ///copy output text
    /// </summary>
    /// <param name="text"></param>
    public void StrCopy(Text text )
        currentActivity.Call("StrCopy", text.text);

    /// <summary>
    ///The method name should be consistent with that specified in Android Java code
    /// </summary>
    public void OnResult(string s)
        resultText.text = s;

    public void onError(string s)
        errorText.text = s;

10: Package test, pay attention to the package name and XML when packaging!

Published 33 original articles, won praise 4, visited 2985
Private letter follow

Tags: Android Java xml Unity

Posted on Tue, 17 Mar 2020 09:04:07 -0700 by coelex