unity调用android语音识别

  • Post author:
  • Post category:其他


语音识别在android下面很简单,因为语音识别就是google的嘛,呵呵~~~源代码网上也到处都有的,我当时试了一下google的语音识别,当时很慢,据说不够稳定,我又试了一下讯飞的语音识别,相对的确讯飞要快点,但是在调用他的mcs.jar包时,在eclipse运行正常,但是在unity调用的时候,我发现它的几个方法都能找到,但是不能调用,我也无语了,没办法就只能用google的技术了哦;而且都有3种方法哦,想详细了解就去这看看吧

http://mysuperbaby.iteye.com/blog/1436754

。我用了第三种方法,因为我不想显示那个对话窗口。我特别要谢谢雨松MOMO,雨松哥的那篇unity和android交互的文章很有用

http://www.xuanyusong.com/archives/667

。废话不对说了,我就贴一下android工程的代码和unity的代码,其他的你们参照雨松哥的那篇文章操作吧,呵呵~~~~

UnityTestActivity.java;

package com.xys;

import java.util.ArrayList;

import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import com.unity3d.player.UnityPlayerActivity;

public class UnityTestActivity extends UnityPlayerActivity{
    /** Called when the activity is first created. */
	Context mContext = null;
	 private SpeechRecognizer sr;
	 String str;
     private static final String TAG = "Unity";
     private final String ACTION_NAME ="send"; 
     BroadcastReceiver mBroadcastReceiver;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        sr = SpeechRecognizer.createSpeechRecognizer(this);        // 初始化识别工具,得到句柄
        sr.setRecognitionListener(new listener());         // 注册回调类及函数
        str ="杨纯说:";       
        sr.startListening(new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS));
        mBroadcastReceiver = new BroadcastReceiver(){ 
            @Override 
            public void onReceive(Context context, Intent intent) {            	
                String action = intent.getAction(); 
                Log.i(action, action);              
                if(action.equals(ACTION_NAME)){ 
                    SpeechRecognizer sr2;
                    sr2 = SpeechRecognizer.createSpeechRecognizer(mContext);        // 初始化识别工具,得到句柄
                    sr2.setRecognitionListener(new listener());         // 注册回调类及函数
                	sr2.startListening(new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS));
                } 
            } 
             
        }; 
		 //注册广播 
        registerBoradcastReceiver(); 
    }
    public void registerBoradcastReceiver(){ 
        IntentFilter myIntentFilter = new IntentFilter(); 
        myIntentFilter.addAction(ACTION_NAME); 
        //注册广播       
        registerReceiver(mBroadcastReceiver, myIntentFilter); 
    } 

    public String getstr(){
    	return str;
    }
    public void StartActivity0(){}
    class listener implements RecognitionListener            // 回调类的实现
    {
             public void onReadyForSpeech(Bundle params)
             {
                      Log.d(TAG, "onReadyForSpeech");
             }
             public void onBeginningOfSpeech()
             {
                      Log.d(TAG, "onBeginningOfSpeech");
             }
             public void onRmsChanged(float rmsdB)
             {
                      Log.d(TAG, "onRmsChanged");
             }
             public void onBufferReceived(byte[] buffer)
             {
                      Log.d(TAG, "onBufferReceived");
             }
             public void onEndOfSpeech()
             {
                      Log.d(TAG, "onEndofSpeech");
             }
             public void onError(int error)
             {
            	 Log.d(TAG,  "error " +  error);
                 if(error!=0){
               	  Intent mIntent = new Intent(ACTION_NAME); 
                     mIntent.putExtra("yaer", "发送广播,相当于在这里传送数据");  
                     //发送广播 
                     sendBroadcast(mIntent); 
                 }
             }
             public void onResults(Bundle results)     // 返回识别到的数据
             {
                 String s="";    
            	 Log.d(TAG, "onResults " + results);
                 ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                 for (int i = 0; i < data.size(); i++)
                 {
                           Log.d(TAG, "result " + data.get(i));
                           s += data.get(i);                           
                 }
                 str=s;
                 if(data.size()>0){
                    Intent mIntent = new Intent(ACTION_NAME); 
                    mIntent.putExtra("yaer", "发送广播,相当于在这里传送数据");  
                    //发送广播 
                    sendBroadcast(mIntent); 
                } 
             }
             public void onPartialResults(Bundle partialResults)
             {
            	      
                      Log.d(TAG, "onPartialResults");
             }
             public void onEvent(int eventType, Bundle params)
             {
                      Log.d(TAG, "onEvent " + eventType);
             }
    }
     
}

manifest.xml

<manifest
	xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xys"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
   <uses-permission android:name="android.permission.INTERNET"/> 
   <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".UnityTestActivity"
            android:label="@string/title_activity_unity_test">

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

</manifest>

下面是unity工程代码:

Test.cs

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour
{
	
	string stringToEdit=""; 
	public GUISkin skin;		
	void Awake () {
	    DontDestroyOnLoad (transform.gameObject);

    }
	// Update is called once per frame
	void Update ()
	{
		if (Input.GetKeyDown(KeyCode.Escape)){
   			Application.Quit();
  		}
	}
	void OnGUI()
	{		
		AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
	        AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
		stringToEdit=jo.Call<string>("getstr");
		GUI.skin=skin;
		if(GUI.Button(new Rect(Screen.width*0.8F,0,Screen.width*0.2F,50),"open"))
		{
			 if(Application.platform==RuntimePlatform.Android){								
	        	jo.Call("StartActivity0");
			}else{
				 Application.LoadLevel("Scene_03");
			}
		}
		
		GUI.Label(new Rect(0,0,Screen.width*0.8F,Screen.height),""+stringToEdit);
	}
}



版权声明:本文为dlnuchunge原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。