TwiNNeR

speech recognize

Oct 1st, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.34 KB | None | 0 0
  1. package com.gridsystem.nimblehero;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.speech.RecognitionListener;
  6. import android.speech.RecognizerIntent;
  7. import android.speech.SpeechRecognizer;
  8. import android.util.Log;
  9.  
  10. import java.util.ArrayList;
  11. import java.util.List;
  12.  
  13. /**
  14.  * Created by AtanasK on 6/2/2016.
  15.  */
  16. public class SpeechClass implements RecognitionListener, SpeechInterface {
  17.     private ArrayList<String> guesses;
  18.  
  19.     public SpeechRecognizer speech = null;
  20.     private Intent recognizerIntent;
  21.     private String LOG_TAG = "VoiceRecognitionActivity";
  22.  
  23.     private List<String> matches;
  24.  
  25.     public SpeechClass() {
  26.         // setup na recognizer potrebni raboti
  27.         speech = SpeechRecognizer.createSpeechRecognizer(AndroidLauncher.getAppContext());
  28.         speech.setRecognitionListener(this);
  29.         recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  30.         recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
  31.         recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "com.gridsystem.nimblehero");
  32.         recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
  33.         recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 7);
  34.  
  35.         matches = new ArrayList<>();
  36.     }
  37.  
  38.     @Override
  39.     public void onReadyForSpeech(Bundle params) {
  40.  
  41.     }
  42.  
  43.     @Override
  44.     public void onBeginningOfSpeech() {
  45.  
  46.     }
  47.  
  48.     @Override
  49.     public void onRmsChanged(float rmsdB) {
  50.  
  51.     }
  52.  
  53.     @Override
  54.     public void onBufferReceived(byte[] buffer) {
  55.  
  56.     }
  57.  
  58.     @Override
  59.     public void onEndOfSpeech() {
  60.  
  61.     }
  62.  
  63.     @Override
  64.     public void onError(int error) {
  65.         String errorMessage = getErrorText(error);
  66.         if (errorMessage.equals("No speech input"))
  67. //            answerView.setText("You didn't say anything... Try again!");
  68.             if (errorMessage.equals("No match"))
  69. //            answerView.setText("You got it wrong... Try again!");
  70.  
  71.                 Log.d(LOG_TAG, "FAILED " + errorMessage);
  72.     }
  73.  
  74.     @Override
  75.     public void onResults(Bundle results) {
  76. //        Log.i(LOG_TAG, "onResults");
  77.         matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
  78. //
  79. //        for (String s : matches) {
  80. //            if (game.checkAnswer(s)) {
  81. //                game.scoreUp();
  82. //                updateQuestion();
  83. //                updateAnswer(true, s);
  84. //                return;
  85. //            }
  86. //        }
  87. //        game.missedQuestion();
  88. //        updateAnswer(false, null);
  89.     }
  90.  
  91.     @Override
  92.     public void onPartialResults(Bundle partialResults) {
  93.  
  94.     }
  95.  
  96.     @Override
  97.     public void onEvent(int eventType, Bundle params) {
  98.  
  99.     }
  100.  
  101.     public static String getErrorText(int errorCode) {
  102.         String message;
  103.         switch (errorCode) {
  104.             case SpeechRecognizer.ERROR_AUDIO:
  105.                 message = "Audio recording error";
  106.                 break;
  107.             case SpeechRecognizer.ERROR_CLIENT:
  108.                 message = "Client side error";
  109.                 break;
  110.             case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
  111.                 message = "Insufficient permissions";
  112.                 break;
  113.             case SpeechRecognizer.ERROR_NETWORK:
  114.                 message = "Network error";
  115.                 break;
  116.             case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
  117.                 message = "Network timeout";
  118.                 break;
  119.             case SpeechRecognizer.ERROR_NO_MATCH:
  120.                 message = "No match";
  121.                 break;
  122.             case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
  123.                 message = "RecognitionService busy";
  124.                 break;
  125.             case SpeechRecognizer.ERROR_SERVER:
  126.                 message = "error from server";
  127.                 break;
  128.             case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
  129.                 message = "No speech input";
  130.                 break;
  131.             default:
  132.                 message = "Didn't understand, please try again.";
  133.                 break;
  134.         }
  135.  
  136.         return message;
  137.     }
  138.  
  139.     @Override
  140.     public void startSpeech() {
  141.         speech.startListening(recognizerIntent);
  142.     }
  143.  
  144.     @Override
  145.     public List<String> results() {
  146.         return matches;
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment