Unity calls Voice dictation (Android Studio Edition)

Source: Internet
Author: User

I used to do a year of Android development, has been using eclipse, but as is the mainstream, learning a wave as, and then found that many will not operate. Tidying up the work of yesterday's Day is also a kind of a.

First go to the flying open platform to register your account, then go to the console to create an app, add the required services, and then download the SDK. This process has been a sentence, should read this article no one will not.

Say how to use the flying and packaging into AAR in as.

1. Create a project

* Because the layout file is not available, it is not necessary to generate it.

2. Add a library

3. Add the relevant library file

Find Unity's Classes.jar in this position, note the information in the green box, and if your unity uses the mono frame, use this, and if it's il2cpp, it's

Then find the SDK you just downloaded and find the two files under Libs.

*RMEABI-V7A is adapted according to the installed machine, you can use the other libraries under the Libs yourself

Copy the above three files to Flytek4unity/libs

Add the Msc.jar and Classes.jar as library files to the new module, not the app. If the right-click menu does not show this item, we can right-click on the flytek4unity

Select library dependencies that could not be added.

3. Encoding

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 classMainactivity extends Unityplayeractivity {SpeechRecognizer miat; String Voiceresult=""; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Speechutility.createutility ( This, Speechconstant.appid +"=59eea647"); Miat= Speechrecognizer.createrecognizer ( This, Minitlistener); }         Public voidstartlistening () {miat.setparameter (Speechconstant.domain,"IAT"); Miat.setparameter (Speechconstant.language,"en_US"); Miat.setparameter (Speechconstant.key_speech_timeout,"4000"); Miat.setparameter (Speechconstant.vad_eos," -"); Miat.setparameter (SPEECHCONSTANT.ASR_PTT,"0"); intRET =miat.startlistening (Recognizerlistener); Unityplayer.unitysendmessage ("Iflytek","onstartlistening", string.valueof (ret)); }         PrivateInitlistener Minitlistener =NewInitlistener () {@Override Public voidOnInit (intarg0) {Unityplayer.unitysendmessage ("Iflytek","onstartlistening", String.valueof (arg0));          }    }; PrivateRecognizerlistener Recognizerlistener =NewRecognizerlistener () {@Override Public voidOnvolumechanged (intARG0,byte[] arg1) {            //TODO auto-generated Method Stub} @Override Public voidOnresult (Recognizerresult arg0, Boolean arg1) {Voiceresult+=Jsonparser.parseiatresult (arg0.getresultstring ()); if(arg1) {unityplayer.unitysendmessage ("Iflytek","Onresult", Voiceresult); Voiceresult="";            Miat.stoplistening (); }} @Override Public voidOnEvent (intARG0,intArg1,intarg2, Bundle arg3) {            //TODO auto-generated Method Stub} @Override Public voidonError (Speecherror arg0) {unityplayer.unitysendmessage ("Iflytek","OnError", Arg0.geterrordescription ()); } @Override Public voidOnendofspeech () {Unityplayer.unitysendmessage ("Iflytek","Onendofspeech",""); } @Override Public voidOnbeginofspeech () {Unityplayer.unitysendmessage ("Iflytek","Onbeginofspeech",""); }    };}

Create a Java class under Src/main/java, write the above code, the emphasis is AppID, the following string to fill the open platform you apply for the application ID, and then is the Startlistening method in the series parameters, this welcome to the news Fly official website to find information, not detailed speaking.

Then you will encounter Jsonparser no problem, this please download the Flight SDK compression package to find

* Watch the path of the red box

4. Modify Manifest

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Cn.egstudio.flytek4unity"Android:versioncode= "1"Android:versionname= "1.0" >    <uses-permissionAndroid:name= "Android.permission.RECORD_AUDIO" />    <uses-permissionAndroid:name= "Android.permission.INTERNET" />    <uses-permissionAndroid:name= "Android.permission.ACCESS_NETWORK_STATE" />    <uses-permissionAndroid:name= "Android.permission.ACCESS_WIFI_STATE" />    <uses-permissionAndroid:name= "Android.permission.CHANGE_NETWORK_STATE" />    <uses-permissionAndroid:name= "Android.permission.READ_PHONE_STATE" />    <uses-permissionAndroid:name= "Android.permission.ACCESS_FINE_LOCATION" />    <uses-permissionAndroid:name= "Android.permission.READ_CONTACTS" />    <uses-permissionAndroid:name= "Android.permission.WRITE_EXTERNAL_STORAGE" />    <USES-SDKandroid:minsdkversion= "+"android:targetsdkversion= "All" />    <ApplicationAndroid:allowbackup= "true"Android:label= "@string/app_name">        <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>    </Application></Manifest>

* Add permissions to modify startup activity (this requires no testing, before eclipse, because the export jar and as export AAR are not the same, direct lead to the previous manifest file)

5. Generating AAR

Make a module,aar and create a

Import this AAR and the Androidmanifest.xml under this module into unity, and remember that it must be the androidmanifest of the module that you just modified, not the app's

A structure diagram in unity.

Encoding in 6.Unity

Creates a Iflytek empty object, and corresponds to the mainactivity.

usingSystem.Collections;usingSystem.Collections.Generic;usingUnityengine;usingUnityengine.ui; Public classIflytek:monobehaviour {PrivateAndroidjavaclass AJC; PrivateAndroidjavaobject Ajo;  PublicButton Startbutton;  PublicText Resulttext; Private voidStart () {AJC=NewAndroidjavaclass ("Com.unity3d.player.UnityPlayer"); Ajo= AJC. Getstatic<androidjavaobject> ("currentactivity"); if(Startbutton) {StartButton.onClick.AddListener ()={startlistening ();}); }    }     Public voidstartlistening () {Ajo. Call ("startlistening"); }     Public voidOnstartlistening (stringret) {        intresult =int.        Parse (ret); Startbutton.interactable= result = =0; }     Public voidOnresult (stringresult) {Resulttext.text=result; }     Public voidOnError (stringerrormessage) {Resulttext.text=errormessage; }     Public voidOnendofspeech (stringempty) {Startbutton.getcomponentinchildren<Text> (). Text ="closed, click to listen"; Startbutton.interactable=true; }     Public voidOnbeginofspeech (stringempty) {Startbutton.getcomponentinchildren<Text> (). Text ="listen to ing"; Startbutton.interactable=false; }}

Create a script, hanging under the Iflytek, many of the methods are as callback exist, specifically see mainactivity

Then create a button and text in the scene to trigger dictation and receive dictation results.

7. Release

These must be matched, Unity's package name and imported manifest file registration, minimum API support level consistent, Classes.jar with mono or il2cpp and script backend consistent

Done. No real machine, trouble.

Unity calls Voice dictation (Android Studio Edition)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.