Created
January 27, 2015 07:01
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cst.mikunade; | |
import java.util.ArrayList; | |
import android.speech.RecognitionListener; | |
import android.speech.RecognizerIntent; | |
import android.speech.SpeechRecognizer; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.os.Bundle; | |
import android.util.Log; | |
import com.unity3d.player.UnityPlayerActivity; | |
public class MainActivity extends UnityPlayerActivity { | |
Context mContext = null; | |
SpeechRecognizer sr; | |
String str; | |
private static final String TAG = "Unity"; | |
private final String ACTION_NAME = "send"; | |
BroadcastReceiver mBroadcastReceiver; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
mContext = this; | |
str = ""; | |
registerBroadcastReceiver(); | |
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)); | |
} | |
} | |
}; | |
registerBroadcastReceiver(); | |
} | |
public void MyRecogStart(){ | |
Intent mIntent = new Intent(ACTION_NAME); | |
sendBroadcast(mIntent); | |
} | |
public void registerBroadcastReceiver(){ | |
IntentFilter myIntentFilter = new IntentFilter(); | |
myIntentFilter.addAction(ACTION_NAME); | |
registerReceiver(mBroadcastReceiver,myIntentFilter); | |
} | |
public String getStr(){ | |
return str; | |
} | |
class listener implements RecognitionListener{ | |
@Override | |
public void onReadyForSpeech(Bundle params) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onBeginningOfSpeech() { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onRmsChanged(float rmsdB) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onBufferReceived(byte[] buffer) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onEndOfSpeech() { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onError(int error) { | |
Log.d(TAG,"Error" + error); | |
if(error != 0){ | |
Intent mIntent = new Intent(ACTION_NAME); | |
sendBroadcast(mIntent); | |
} | |
} | |
@Override | |
public void onResults(Bundle results) { | |
String s = ""; | |
Log.d(TAG,"result" + results); | |
ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); | |
if(data.size() >0){ | |
s = (String) data.get(0); | |
str = s; | |
} | |
} | |
@Override | |
public void onPartialResults(Bundle partialResults) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onEvent(int eventType, Bundle params) { | |
// TODO Auto-generated method stub | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for the code!