Unity调用讯飞做语音听写(Android Studio版)

以前做过一年Android开发,一直用的是Eclipse,但是AS是主流了,顺手学一波AS,然后发现很多都不会操作了。整理一下昨天一天的工作成果,也算是抛砖引玉。

先去讯飞开放平台注册帐号,然后去控制台创建应用,添加需要的服务,然后把SDK下载。这个流程一句话带过,应该看这篇文章的没人不会。

说一说怎么在AS里面使用讯飞和打包成AAR吧。

1.创建一个项目 

*因为用不到布局文件,所以没必要生成。

 

2.添加一个库

3.添加相关的库文件

在这个位置找到Unity的Classes.jar,注意绿框里的信息,如果你的Unity使用的是MONO框架就用这个,如果是IL2CPP的话,就是下图

 

然后找到刚才下载的SDK,找libs下的这两个文件

*rmeabi-v7a是根据安装的机器来做适配的,你可以自行使用libs下的其它库

将以上三个文件Copy到flytek4unity/libs下

 

给MSC.jar和classes.jar以库文件的形式添加到新增的模块,非app。如果右键菜单不显示这个项,我们可以在flytek4unity右键

 

选择未能添加的库依赖。

 

 3.编码

package cn.egstudio.flytek4unity;

import com.iflytek.cloud.InitListener;
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.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;

import android.app.Activity;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends UnityPlayerActivity {
    SpeechRecognizer mIAT;
    String voiceResult = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SpeechUtility.createUtility(this, SpeechConstant.APPID + "=59eea647");
        mIAT = SpeechRecognizer.createRecognizer(this, mInitListener);
    }
    
    public void StartListening() {
        mIAT.setParameter(SpeechConstant.DOMAIN,"iat");
        mIAT.setParameter(SpeechConstant.LANGUAGE,"en_us");
        mIAT.setParameter(SpeechConstant.KEY_SPEECH_TIMEOUT,"4000");
        mIAT.setParameter(SpeechConstant.VAD_EOS,"2000");
        mIAT.setParameter(SpeechConstant.ASR_PTT,"0");
        int ret = mIAT.startListening(recognizerListener);
        UnityPlayer.UnitySendMessage("iFlyTek", "OnStartListening", String.valueOf(ret));
    } 
    
    private InitListener mInitListener = new InitListener() {
        
        @Override
        public void onInit(int arg0) {
            UnityPlayer.UnitySendMessage("iFlyTek", "OnStartListening", String.valueOf(arg0));
        }
    };  
    
    private RecognizerListener recognizerListener = new RecognizerListener() {
        
        @Override
        public void onVolumeChanged(int arg0, byte[] arg1) {
            // TODO Auto-generated method stub
            
        }
        
        @Override
        public void onResult(RecognizerResult arg0, boolean arg1) {
            voiceResult += JsonParser.parseIatResult(arg0.getResultString());
            if(arg1){
                UnityPlayer.UnitySendMessage("iFlyTek", "OnResult", voiceResult);
                voiceResult = "";
                mIAT.stopListening();
            }
        }
        
        @Override
        public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
            // TODO Auto-generated method stub
            
        }
        
        @Override
        public void onError(SpeechError arg0) {
            UnityPlayer.UnitySendMessage("iFlyTek", "OnError", arg0.getErrorDescription());
        }
        
        @Override
        public void onEndOfSpeech() {
            UnityPlayer.UnitySendMessage("iFlyTek", "OnEndOfSpeech","");
        }
        
        @Override
        public void onBeginOfSpeech() {
            UnityPlayer.UnitySendMessage("iFlyTek", "OnBeginOfSpeech","");
        }
    };
}

在src/main/java下创建一个java类,写上如上代码,重点是APPID,后面的字符串填上讯飞开放平台你申请的应用ID,然后就是StartListening方法中系列参数,这个欢迎去讯飞官网找资料,不详细讲。

 然后你会遇到JsonParser没有的问题,这个请在下载讯飞SDK压缩包找找

*注意看红框的路径

4.修改Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.egstudio.flytek4unity"
    android:versionCode="1"
    android:versionName="1.0" >
    <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-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="23" />

    <application
        android:allowBackup="true"
        android:label="@string/app_name">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

*添加权限,修改启动Activity(这个需不需要我没测试,之前在Eclipse的时候,因为导出jar和AS导出AAR不一样,直接导了之前的清单文件)

5.生成AAR

 

Make一下Module,aar就生成了

将这个aar和这个模块下的androidmanifest.xml导入Unity,记住,一定是刚刚修改的这个模块下的androidmanifest,不是app的

 

Unity中的结构图。

6.Unity中的编码

创建一个iFlyTek的空物体,和MainActivity中的相对应。

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

public class iFlyTek : MonoBehaviour {

    private AndroidJavaClass ajc;
    private AndroidJavaObject ajo;

    public Button StartButton;
    public Text ResultText;

    private void Start() {
        ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        ajo = ajc.GetStatic<AndroidJavaObject>("currentActivity");
        if (StartButton) {
            StartButton.onClick.AddListener(() => { StartListening(); });
        }
    }

    public void StartListening() {
        ajo.Call("StartListening");
    }

    public void OnStartListening(string ret) {
        int result = int.Parse(ret);
        StartButton.interactable = result == 0;
    }

    public void OnResult(string result) {
        ResultText.text = result;
    }

    public void OnError(string errorMessage) {
        ResultText.text = errorMessage;
    }

    public void OnEndOfSpeech() {
        StartButton.GetComponentInChildren<Text>().text = "已结束,点击聆听";
        StartButton.interactable = true;
    }

    public void OnBeginOfSpeech() {
        StartButton.GetComponentInChildren<Text>().text = "聆听ing";
        StartButton.interactable = false;
    }
}

创建一个脚本,挂在iFlyTek下,里面很多方法是作为callback存在的,具体看MainActivity

然后在场景创建一个Button和Text,用来触发听写和接收听写结果。

 

7.发布

 

这些都要能匹配的上,Unity的包名和导入的manifest文件报名一致,最小API支持等级得一致,classes.jar用mono还是IL2CPP得和Script Backend一致

 

 大功告成。不发真机截图了,麻烦。

 

转载于:https://www.cnblogs.com/CodeSnippet/p/7735898.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
About Wouldn't your prefer to let your users speak instead of making them type? This plugin uses OS components for speech recognition and send it to your Unity scripts as String objects. Plugin supports: - Android >= 3.0 (haven’t tested below, it might work though… ), - iOS >= 10.0. That doesn’t mean you can’t target iOS lower than 10 - you simply have to prepare fallback code to cover cases when user doesn’t have access to speech recognition (SpeechRecognizer.EngineExists() for the help!). Keep in mind that both iOS and Android might use Internet connection for speech detection, which means it might fail in case there’s no active connection. Plugin doesn’t work in Editor! You have to run your app on real iOS or Android device. MOBILE SPEECH RECOGNIZER - UNITY PLUGIN ?2 Quick Start Open example scene Go to KKSpeechRecognizer/Example folder inside Unity and open ExampleScene: It shows basic usage of a plugin, which is: 1. Detecting if speech recognition exists on user’s device (keep in mind that it won’t be available on e.g. iOS 9 or old Android phones), 2. If it exists, and user clicks on “Start Recording” button it listens for recognized text and displays it on a screen, 3. On Android, speech recognition automatically detects when user finishes speaking, but on iOS we have to wait for user clicking “Stop Recording” to finish whole process (i.e. get final results). Before running it on Android or iOS device you have to… Setup permissions iOS After generating Xcode project (keep in mind that you have to use Xcode 8 or higher) you have to add two permissions keys to your project: MOBILE SPEECH RECOGNIZER - UNITY PLUGIN ?3 NSMicrophoneUsageDescription explanation from Apple docs: This key lets you describe the reason your app accesses any of the the device’s microphones. When the system prompts the user to allow access, this string is displayed as part of the alert. NSSpeechRecognitionUsageDescription explanation from Apple docs: This key lets you describe the reason y
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值