unity语音识别(转)
转:http://blog.csdn.net/nateyang/article/details/7881447
语音识别在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的代码,其他的你们参照雨松哥的那篇文章操作吧,呵呵~~~~
1 package com.xys; 2 3 import java.util.ArrayList; 4 5 import android.os.Bundle; 6 import android.speech.RecognitionListener; 7 import android.speech.RecognizerIntent; 8 import android.speech.SpeechRecognizer; 9 import android.util.Log; 10 import android.content.BroadcastReceiver; 11 import android.content.Context; 12 import android.content.Intent; 13 import android.content.IntentFilter; 14 import com.unity3d.player.UnityPlayerActivity; 15 16 public class UnityTestActivity extends UnityPlayerActivity{ 17 /** Called when the activity is first created. */ 18 Context mContext = null; 19 private SpeechRecognizer sr; 20 String str; 21 private static final String TAG = "Unity"; 22 private final String ACTION_NAME ="send"; 23 BroadcastReceiver mBroadcastReceiver; 24 @Override 25 public void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 mContext = this; 28 sr = SpeechRecognizer.createSpeechRecognizer(this); // 初始化识别工具,得到句柄 29 sr.setRecognitionListener(new listener()); // 注册回调类及函数 30 str ="杨纯说:"; 31 sr.startListening(new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS)); 32 mBroadcastReceiver = new BroadcastReceiver(){ 33 @Override 34 public void onReceive(Context context, Intent intent) { 35 String action = intent.getAction(); 36 Log.i(action, action); 37 if(action.equals(ACTION_NAME)){ 38 SpeechRecognizer sr2; 39 sr2 = SpeechRecognizer.createSpeechRecognizer(mContext); // 初始化识别工具,得到句柄 40 sr2.setRecognitionListener(new listener()); // 注册回调类及函数 41 sr2.startListening(new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS)); 42 } 43 } 44 45 }; 46 //注册广播 47 registerBoradcastReceiver(); 48 } 49 public void registerBoradcastReceiver(){ 50 IntentFilter myIntentFilter = new IntentFilter(); 51 myIntentFilter.addAction(ACTION_NAME); 52 //注册广播 53 registerReceiver(mBroadcastReceiver, myIntentFilter); 54 } 55 56 public String getstr(){ 57 return str; 58 } 59 public void StartActivity0(){} 60 class listener implements RecognitionListener // 回调类的实现 61 { 62 public void onReadyForSpeech(Bundle params) 63 { 64 Log.d(TAG, "onReadyForSpeech"); 65 } 66 public void onBeginningOfSpeech() 67 { 68 Log.d(TAG, "onBeginningOfSpeech"); 69 } 70 public void onRmsChanged(float rmsdB) 71 { 72 Log.d(TAG, "onRmsChanged"); 73 } 74 public void onBufferReceived(byte[] buffer) 75 { 76 Log.d(TAG, "onBufferReceived"); 77 } 78 public void onEndOfSpeech() 79 { 80 Log.d(TAG, "onEndofSpeech"); 81 } 82 public void onError(int error) 83 { 84 Log.d(TAG, "error " + error); 85 if(error!=0){ 86 Intent mIntent = new Intent(ACTION_NAME); 87 mIntent.putExtra("yaer", "发送广播,相当于在这里传送数据"); 88 //发送广播 89 sendBroadcast(mIntent); 90 } 91 } 92 public void onResults(Bundle results) // 返回识别到的数据 93 { 94 String s=""; 95 Log.d(TAG, "onResults " + results); 96 ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 97 for (int i = 0; i < data.size(); i++) 98 { 99 Log.d(TAG, "result " + data.get(i)); 100 s += data.get(i); 101 } 102 str=s; 103 if(data.size()>0){ 104 Intent mIntent = new Intent(ACTION_NAME); 105 mIntent.putExtra("yaer", "发送广播,相当于在这里传送数据"); 106 //发送广播 107 sendBroadcast(mIntent); 108 } 109 } 110 public void onPartialResults(Bundle partialResults) 111 { 112 113 Log.d(TAG, "onPartialResults"); 114 } 115 public void onEvent(int eventType, Bundle params) 116 { 117 Log.d(TAG, "onEvent " + eventType); 118 } 119 } 120 121 }
1 <manifest 2 xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.xys" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="15" /> 10 <uses-permission android:name="android.permission.INTERNET"/> 11 <uses-permission android:name="android.permission.RECORD_AUDIO" /> 12 <application 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name=".UnityTestActivity" 18 android:label="@string/title_activity_unity_test"> 19 20 <intent-filter> 21 <action android:name="android.intent.action.MAIN" /> 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 </application> 26 27 </manifest>
1 using UnityEngine; 2 using System.Collections; 3 4 public class Test : MonoBehaviour 5 { 6 7 string stringToEdit=""; 8 public GUISkin skin; 9 void Awake () { 10 DontDestroyOnLoad (transform.gameObject); 11 12 } 13 // Update is called once per frame 14 void Update () 15 { 16 if (Input.GetKeyDown(KeyCode.Escape)){ 17 Application.Quit(); 18 } 19 } 20 void OnGUI() 21 { 22 AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 23 AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); 24 stringToEdit=jo.Call<string>("getstr"); 25 GUI.skin=skin; 26 if(GUI.Button(new Rect(Screen.width*0.8F,0,Screen.width*0.2F,50),"open")) 27 { 28 if(Application.platform==RuntimePlatform.Android){ 29 jo.Call("StartActivity0"); 30 }else{ 31 Application.LoadLevel("Scene_03"); 32 } 33 } 34 35 GUI.Label(new Rect(0,0,Screen.width*0.8F,Screen.height),""+stringToEdit); 36 } 37 }

浙公网安备 33010602011771号