Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.unitymodule;
- /**
- * Created by Vinamra on 18-Dec-17.
- */
- import android.speech.RecognitionListener;
- import android.speech.RecognizerIntent;
- import android.speech.SpeechRecognizer;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import java.util.ArrayList;
- public class PluginClass extends Activity implements RecognitionListener {
- private static final int REQ_CODE_SPEECH_INPUT = 100;
- private SpeechRecognizer speech = null;
- private Intent recognizerIntent;
- private String LOG_TAG = "PluginClass";
- // private boolean check = false;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- speech = SpeechRecognizer.createSpeechRecognizer(this);
- speech.setRecognitionListener(this);
- recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
- recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
- recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
- recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
- recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
- }
- public void onCheckedListen(boolean check) {
- if (check) {
- speech.startListening(recognizerIntent);
- } else {
- speech.stopListening();
- }
- }
- @Override
- public void onResume() {
- super.onResume();
- }
- @Override
- protected void onPause() {
- super.onPause();
- if (speech != null) {
- speech.destroy();
- Log.i(LOG_TAG, "destroy");
- }
- }
- @Override
- public void onBeginningOfSpeech() {
- Log.i(LOG_TAG, "onBeginningOfSpeech");
- }
- @Override
- public void onBufferReceived(byte[] buffer) {
- Log.i(LOG_TAG, "onBufferReceived: " + buffer);
- }
- @Override
- public void onEndOfSpeech() {
- Log.i(LOG_TAG, "onEndOfSpeech");
- }
- @Override
- public void onError(int errorCode) {
- String errorMessage = getErrorText(errorCode);
- Log.d(LOG_TAG, "FAILED " + errorMessage);
- }
- @Override
- public void onEvent(int arg0, Bundle arg1) {
- Log.i(LOG_TAG, "onEvent");
- }
- @Override
- public void onPartialResults(Bundle arg0) {
- Log.i(LOG_TAG, "onPartialResults");
- }
- @Override
- public void onReadyForSpeech(Bundle arg0) {
- Log.i(LOG_TAG, "onReadyForSpeech");
- }
- @Override
- public void onResults(Bundle results) {
- Log.i(LOG_TAG, "onResults");
- ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
- String text = "";
- for (String result : matches)
- text += result + "\n";
- returnRes(text);
- }
- public static String returnRes(String results){
- return results;
- }
- @Override
- public void onRmsChanged(float rmsdB) {
- Log.i(LOG_TAG, "onRmsChanged: " + rmsdB);
- }
- public static String getErrorText(int errorCode) {
- String message;
- switch (errorCode) {
- case SpeechRecognizer.ERROR_AUDIO:
- message = "Audio recording error";
- break;
- case SpeechRecognizer.ERROR_CLIENT:
- message = "Client side error";
- break;
- case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
- message = "Insufficient permissions";
- break;
- case SpeechRecognizer.ERROR_NETWORK:
- message = "Network error";
- break;
- case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
- message = "Network timeout";
- break;
- case SpeechRecognizer.ERROR_NO_MATCH:
- message = "No match";
- break;
- case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
- message = "RecognitionService busy";
- break;
- case SpeechRecognizer.ERROR_SERVER:
- message = "error from server";
- break;
- case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
- message = "No speech input";
- break;
- default:
- message = "Didn't understand, please try again.";
- break;
- }
- return message;
- }
- }
Add Comment
Please, Sign In to add comment