Skip to content

Instantly share code, notes, and snippets.

@NegishiTakumi
Created January 27, 2015 07:01
Show Gist options
  • Select an option

  • Save NegishiTakumi/ba7d678a13b85317db48 to your computer and use it in GitHub Desktop.

Select an option

Save NegishiTakumi/ba7d678a13b85317db48 to your computer and use it in GitHub Desktop.
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
}
}
}
@T-Kuhn

T-Kuhn commented Jun 21, 2016

Copy link
Copy Markdown

thanks for the code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment