unity调用android语音识别
来源:互联网 发布:list和数组的区别 编辑:程序博客网 时间:2024/05/05 13:14
语音识别在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); } } }
<manifestxmlns: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 framevoid 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);}}
- unity调用android语音识别
- Unity语音识别和语音合成 Android
- unity 语音识别
- Android 调用谷歌语音识别
- 【Android 调用谷歌语音识别】
- Android 调用谷歌语音识别
- Android 调用谷歌语音识别
- Android Studio调用百度语音识别API
- Unity 中使用百度语音识别发布Android程序(一)
- Unity 中使用百度语音识别发布Android程序(二)
- Unity语音识别和语音合成 IOS
- Unity接入语音SDk 语音识别
- unity 使用百度语音进行语音识别
- android语音识别方法一:使用intent调用语音识别程序
- android语音识别方法二:应用程序自己调用语音识别库
- android语音识别方法一:使用intent调用语音识别程序
- android语音识别方法一:使用intent调用语音识别程序
- android语音识别方法二:应用程序自己调用语音识别库
- JNDI实例
- Android MMS 源码目录结构
- android的Touch事件解析(dispatchTouchEvent,onInterceptTouchEvent,onTouchEvent)
- 重装IIS
- HDU 1081 To The Max(最大子矩阵)
- unity调用android语音识别
- android支持html的<input type="file">标签
- 单链表之递归算法
- Perl IDE之Perl Express和Eclipse+EPIC+PadWalker
- 编码和过滤器
- 日志操作利器log4cxx 分享
- 双系统修复Ubuntu 9.10 GRUB 2引导的方法
- NAND FLASH 驱动
- USACO section 2.1 The Castle(dfs)