Guest User

PluginClass.java

a guest
Dec 20th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.57 KB | None | 0 0
  1. package com.example.unitymodule;
  2.  
  3. /**
  4.  * Created by Vinamra on 18-Dec-17.
  5.  */
  6.  
  7. import android.speech.RecognitionListener;
  8. import android.speech.RecognizerIntent;
  9. import android.speech.SpeechRecognizer;
  10. import android.app.Activity;
  11. import android.content.Intent;
  12. import android.os.Bundle;
  13. import android.util.Log;
  14.  
  15. import java.util.ArrayList;
  16.  
  17. public class PluginClass extends Activity implements RecognitionListener {
  18.  
  19.     private static final int REQ_CODE_SPEECH_INPUT = 100;
  20.  
  21.     private SpeechRecognizer speech = null;
  22.     private Intent recognizerIntent;
  23.     private String LOG_TAG = "PluginClass";
  24.     // private boolean check = false;
  25.  
  26.     @Override
  27.     protected void onCreate(Bundle savedInstanceState) {
  28.         super.onCreate(savedInstanceState);
  29.  
  30.         speech = SpeechRecognizer.createSpeechRecognizer(this);
  31.         speech.setRecognitionListener(this);
  32.         recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  33.         recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
  34.         recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
  35.         recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
  36.         recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
  37.     }
  38.  
  39.     public void onCheckedListen(boolean check) {
  40.         if (check) {
  41.             speech.startListening(recognizerIntent);
  42.         } else {
  43.             speech.stopListening();
  44.         }
  45.     }
  46.  
  47.  
  48.  
  49.     @Override
  50.     public void onResume() {
  51.         super.onResume();
  52.     }
  53.  
  54.     @Override
  55.     protected void onPause() {
  56.         super.onPause();
  57.         if (speech != null) {
  58.             speech.destroy();
  59.             Log.i(LOG_TAG, "destroy");
  60.         }
  61.  
  62.     }
  63.  
  64.     @Override
  65.     public void onBeginningOfSpeech() {
  66.         Log.i(LOG_TAG, "onBeginningOfSpeech");
  67.     }
  68.  
  69.     @Override
  70.     public void onBufferReceived(byte[] buffer) {
  71.         Log.i(LOG_TAG, "onBufferReceived: " + buffer);
  72.     }
  73.  
  74.     @Override
  75.     public void onEndOfSpeech() {
  76.         Log.i(LOG_TAG, "onEndOfSpeech");
  77.     }
  78.  
  79.     @Override
  80.     public void onError(int errorCode) {
  81.         String errorMessage = getErrorText(errorCode);
  82.         Log.d(LOG_TAG, "FAILED " + errorMessage);
  83.     }
  84.  
  85.     @Override
  86.     public void onEvent(int arg0, Bundle arg1) {
  87.         Log.i(LOG_TAG, "onEvent");
  88.     }
  89.  
  90.     @Override
  91.     public void onPartialResults(Bundle arg0) {
  92.         Log.i(LOG_TAG, "onPartialResults");
  93.     }
  94.  
  95.     @Override
  96.     public void onReadyForSpeech(Bundle arg0) {
  97.         Log.i(LOG_TAG, "onReadyForSpeech");
  98.     }
  99.  
  100.     @Override
  101.     public void onResults(Bundle results) {
  102.         Log.i(LOG_TAG, "onResults");
  103.         ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
  104.         String text = "";
  105.         for (String result : matches)
  106.             text += result + "\n";
  107.  
  108.         returnRes(text);
  109.  
  110.     }
  111.  
  112.     public static String returnRes(String results){
  113.         return results;
  114.     }
  115.  
  116.     @Override
  117.     public void onRmsChanged(float rmsdB) {
  118.         Log.i(LOG_TAG, "onRmsChanged: " + rmsdB);
  119.     }
  120.  
  121.     public static String getErrorText(int errorCode) {
  122.         String message;
  123.         switch (errorCode) {
  124.             case SpeechRecognizer.ERROR_AUDIO:
  125.                 message = "Audio recording error";
  126.                 break;
  127.             case SpeechRecognizer.ERROR_CLIENT:
  128.                 message = "Client side error";
  129.                 break;
  130.             case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
  131.                 message = "Insufficient permissions";
  132.                 break;
  133.             case SpeechRecognizer.ERROR_NETWORK:
  134.                 message = "Network error";
  135.                 break;
  136.             case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
  137.                 message = "Network timeout";
  138.                 break;
  139.             case SpeechRecognizer.ERROR_NO_MATCH:
  140.                 message = "No match";
  141.                 break;
  142.             case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
  143.                 message = "RecognitionService busy";
  144.                 break;
  145.             case SpeechRecognizer.ERROR_SERVER:
  146.                 message = "error from server";
  147.                 break;
  148.             case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
  149.                 message = "No speech input";
  150.                 break;
  151.             default:
  152.                 message = "Didn't understand, please try again.";
  153.                 break;
  154.         }
  155.         return message;
  156.     }
  157.  
  158.  
  159. }
Add Comment
Please, Sign In to add comment