3D Voice Weather Ball--using Android voice service in Unity

Source: Internet
Author: User

Reprint Please indicate this article from Big Corn's blog (http://blog.csdn.net/a396901990 ), thank you for your support!


Opening nonsense:


This project is prepared in four parts:

One: Create rotatable "3D ball ":3D Voice Weather ball (source sharing)-- Create rotatable 3D ball

second: Through the weather Service, from the network to obtain real weather information and dynamic generation "3D Ball ": 3D Voice Weather ball (source sharing)--create 3D ball dynamically through weather service

Three: Android voice service and Unity messaging

IV: combination of Unity3d end and Android terminal

The first two articles have described how to create this 3D ball, this article describes how to use the Android voice service in Unity, and the last article will show you how to control the 3D ball with sound.

On the left is Unity made and runs on the computer (the effect that this section needs to achieve)

On the right is unity with Android and voice control after running on the phone (all the final results after the introduction):



Voice Services:

I use the Voice service is Iflytek Voice , their official website is Http://open.voicecloud.cn/index.php/default/speechservice

Go to the official website to download the Android version of the Voice SDK (need to register and some rotten seven or eight bad things, a little trouble)

After the download there are some open packages and a use demo, this demo run the effect of the following:



Introduction to use:

I only use voice dictation and speech synthesis , the following is a brief introduction to the use of these two functions.


There are some "initialization" tasks that need to be used:

Set some permissions in Androidmanifest.xml:

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

To import a development package:

Armeabiso dynamic library

mac. Jar Jar Package


Set permissions in code:

Speechutility.createutility (This, speechconstant.appid + "=540dcea0");

Voice Dictation:

is to convert words into words. The recognition rate is very accurate, basically no fault.

Initialize the Recognition object:

Initialize the recognition object SpeechRecognizer Mvoice = Speechrecognizer.createrecognizer (this, minitlistener);

Set parameters:

Set Language Mvoice.setparameter (Speechconstant.language, "ZH_CN");//Set Language area mvoice.setparameter (Speechconstant.accent, " Mandarin ");//Set the voice front end point Mvoice.setparameter (Speechconstant.vad_bos," 4000 ");//Set the voice after endpoint Mvoice.setparameter ( Speechconstant.vad_eos, "1000");//Set Punctuation mvoice.setparameter (SPEECHCONSTANT.ASR_PTT, "0");// Set Audio save path Mvoice.setparameter (Speechconstant.asr_audio_path, "/SDCARD/IFLYTEK/WAVAUDIO.PCM");
To set up a dictation listener:
Private Recognizerlistener recognizerlistener=new Recognizerlistener () {@Overridepublic void Onbeginofspeech () { Showtip ("Start Talking");} @Overridepublic void OnError (speecherror error) {Showtip (Error.getplaindescription (True));} @Overridepublic void Onendofspeech () {Showtip ("End of Speech");} @Overridepublic void Onresult (Recognizerresult results, Boolean islast) {log.d (TAG, results.getresultstring ()); String Text = Jsonparser.parseiatresult (results.getresultstring ()); Mresulttext.append (text); Mresulttext.setselection (Mresulttext.length ()); if (islast) {//todo final result}} @Overridepublic void onvolumechanged (int Volume) {showtip ("Currently speaking, Volume Size:" + volume);} @Overridepublic void onEvent (int eventtype, int arg1, int arg2, Bundle obj) {}};
Call:
Mvoice.startlistening (Voicelistener);


Speech synthesis:

Convert text to Speech and read it.

The use of the method and the above speech recognition is very similar, we can see the code, here I will not waste everyone's time.

You can choose the speaker's gender when setting parameters, and you can also choose a dialect.

I used to use dialect to synthesize a bit of curse, listen to the special Siao ...

PS: I'm just a very simple introduction, if you really want to use the recommended sample code with the document (can be found in the download of the compressed package) to do a good look.



Using the Android Voice service in Unity:

Here's a quick introduction to how to use the voice service, and now the question is how to invoke the service in unity.

The idea is to put the Android project as a package/service/plugin into the Unity project so that we can call the Android method in unity.

Here's what you need to know about the unity and Android project, which I wrote about before:

Embedded Unity3d view in Android app (3D model)



Android Side code:

All we need to do is make Android's activity inherit from Unityplayeractivity.

Below I put the code of the Android side, combined with the above description of the content I believe you can understand:

public class Mainactivity extends Unityplayeractivity {//Four buttons Private button Voicebutton;private button Detailbutton; Private button Returnbutton;private button quitbutton;private map<string, string> mapallnameid;boolean isFaild = false;//speech result String Voiceresult = null;//All municipalities private string[] strnamepro;//all cities private string[][] strnamecity;//voice dictation to Like private SpeechRecognizer mvoice;//speech synthesis Object Private SpeechSynthesizer mtts;//default pronunciation person private String voicer = "XiaoYan";//Engine class Type private String menginetype = Speechconstant.type_cloud; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.test); View Playerview = Munityplayer.getview (); LinearLayout ll = (linearlayout) Findviewbyid (r.id.unity_layout); Ll.addview (Playerview); Speechutility.createutility (This, speechconstant.appid + "=540dcea0");//Initialize recognition object Mvoice = Speechrecognizer.createrecognizer (this, minitlistener);//Initialize composition object MTTs = Speechsynthesizer.createsynthesizer (This, MttsinitListener); Voicebutton = (Button) Findviewbyid (R.ID.VOICE_BTN); Voicebutton.setonclicklistener (new Voicelistener ()); Returnbutton = (Button) Findviewbyid (R.ID.RETURN_BTN); Returnbutton.setonclicklistener (new Returnlistener ()); Detailbutton = (Button) Findviewbyid (r.id.detail_btn);d Etailbutton.setonclicklistener (New Detaillistener ()); Quitbutton = (Button) Findviewbyid (R.ID.QUIT_BTN); Quitbutton.setonclicklistener (new Quitlistener ()); InitVar ();} public class Voicelistener implements Onclicklistener {@Overridepublic void OnClick (View arg0) {voiceresult = "";//Set parameter SE Tparam (); mvoice.startlistening (Voicelistener);}} public class Returnlistener implements Onclicklistener {@Overridepublic void OnClick (View arg0) { Unityplayer.unitysendmessage ("Main Camera", "Back", "");}} public class Detaillistener implements Onclicklistener {@Overridepublic void OnClick (View arg0) { Unityplayer.unitysendmessage ("Main Camera", "detail", "" ");}} public class Quitlistener implements Onclicklistener {@Overridepublic void onClick (View arg0) {system.exit (0);}} public void Quitapp (String str) {Toast.maketext (Getapplicationcontext (), "Exit", Toast.length_short). Show (); System.exit (0);} Private Recognizerlistener Voicelistener = new Recognizerlistener () {@Overridepublic void Onbeginofspeech () { Toast.maketext (Getapplicationcontext (), "Start Talking", Toast.length_short). Show (); @Overridepublic void OnError (speecherror error) {Toast.maketext (Getapplicationcontext (), "Error", Toast.length_short ). Show ();} @Overridepublic void Onendofspeech () {Toast.maketext (Getapplicationcontext (), "End Speech", Toast.length_short). Show (); @Overridepublic void Onresult (Recognizerresult results, Boolean islast) {Voiceresult = Voiceresult + Jsonparser.parseiatresult (Results.getresultstring ()); if (islast) {Setspeakparam (); Mtts.startspeaking (CheckResult (Voiceresult), mttslistener);//Unityplayer.unitysendmessage ("Main Camera", "Voice", GetResults (Voiceresult));}} @Overridepublic void onvolumechanged (int volume) {//Toast.maketext (Getapplicationcontext (), "whenBefore being spoken, volume size: "+ volume, Toast.length_short)." Show ();} @Overridepublic void onEvent (int eventtype, int arg1, int arg2, Bundle obj) {}};/** * Synthetic callback listener. */private Synthesizerlistener Mttslistener = new Synthesizerlistener () {@Overridepublic void Onspeakbegin () {}@ overridepublic void onspeakpaused () {} @Overridepublic void onspeakresumed () {} @Overridepublic void onbufferprogress ( int percent, int beginpos, int endpos, String info) {} @Overridepublic void onspeakprogress (int percent, int beginpos, int Endpos) {} @Overridepublic void oncompleted (speecherror error) {if (error = = null) {if (!isfaild) {//Send voice to unity results UNITYPL Ayer. Unitysendmessage ("Main Camera", "Voice", Voiceresult);}} else if (Error! = null) {Toast.maketext (Getapplicationcontext (), "error", Toast.length_short). Show ();}} @Overridepublic void onEvent (int eventtype, int arg1, int arg2, Bundle obj) {}};//set parameters for speech recognition public void SetParam () {//Set language Word mvoice.setparameter (speechconstant.language, "ZH_CN");//Set Language area Mvoice.setparameter (SpeechconstanT.accent, "Mandarin");//Set the voice front end point Mvoice.setparameter (Speechconstant.vad_bos, "4000");//Set the Voice post endpoint Mvoice.setparameter ( Speechconstant.vad_eos, "1000");//Set Punctuation mvoice.setparameter (SPEECHCONSTANT.ASR_PTT, "0");// Set the audio save path Mvoice.setparameter (Speechconstant.asr_audio_path, "/SDCARD/IFLYTEK/WAVAUDIO.PCM");} Set speech synthesis parameters private void Setspeakparam () {//Set composite if (Menginetype.equals (Speechconstant.type_cloud)) {Mtts.setparameter (Speechconstant.engine_type, Speechconstant.type_cloud);//Set pronunciation in person mtts.setparameter (Speechconstant.voice_name, Voicer);} else {mtts.setparameter (speechconstant.engine_type, speechconstant.type_local);//Set pronounce person voicer to empty by default by voice + interface to specify the pronunciation of people. Mtts.setparameter (Speechconstant.voice_name, "");} Set the speech speed Mtts.setparameter (speechconstant.speed, "50");//Set Pitch Mtts.setparameter (Speechconstant.pitch, "50");// Set the volume Mtts.setparameter (speechconstant.volume, "50");//Set the player audio stream type Mtts.setparameter (Speechconstant.stream_type, "3" );} /** * Initialize the listener. */private Initlistener Minitlistener = new Initlistener () {@Overridepublic void OnInit (int code) {if (Code! = errorcode.success) {Toast.maketext (Getapplicationcontext (), "Initialization failed, error code:" + C Ode, Toast.length_short). Show ();}}};/ * * Initial monitoring. */private Initlistener Mttsinitlistener = new Initlistener () {@Overridepublic void onInit (int code) {if (Code! = ErrorCode . SUCCESS) {Toast.maketext (Getapplicationcontext (), "Initialization failed, error code:" + code, Toast.length_short). Show ();}};}


The above is not all the code, the Android side of all the code I have uploaded to GitHub:

Https://github.com/a396901990/3D_Sphere/tree/feature/Voice_Weather_3D_Sphere

The 3DVoiceWeather file in the project is an Android project that you can import into eclipse to view.


The above code is already complete code, in accordance with the method in the online tutorial, the Android project in the form of plug-ins into unity, and finally in unity to build an APK can be used in the phone.

How to use voice control for 3D ball rotation I'll cover it in the last article.

3D Voice Weather Ball--using Android voice service in Unity

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.