Advertisement
Guest User

SpeechToText

a guest
Mar 9th, 2019
183
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.rafaelrp.melo_birthday;
  2.  
  3. import android.Manifest;
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.speech.RecognitionListener;
  8. import android.speech.RecognizerIntent;
  9. import android.speech.SpeechRecognizer;
  10. import android.support.v4.app.ActivityCompat;
  11. import android.widget.Toast;
  12.  
  13. import java.util.ArrayList;
  14. import java.util.Locale;
  15.  
  16. public class SpeechToText implements RecognitionListener {
  17.  
  18.     private SpeechRecognizer speech = null;
  19.     private Intent recognizerIntent;
  20.     private MainActivity mainActivity;
  21.     private String text = "";
  22.     private boolean isListening = false;
  23.     private int MINIMUM_LENGTH_FOR_EXTRA_SPEECH_IN_MILLIS = 3000;
  24.     private final backgroundVoiceListener backgroundListener;
  25.  
  26.     public boolean isListening() {
  27.         return isListening;
  28.     }
  29.  
  30.     public void setListening(boolean listening) {
  31.         isListening = listening;
  32.     }
  33.  
  34.     public  SpeechToText(MainActivity activity) {
  35.         mainActivity = activity;
  36.         backgroundListener = new backgroundVoiceListener();
  37.         speech = SpeechRecognizer.createSpeechRecognizer(activity);
  38.         speech.setRecognitionListener(this);
  39.         recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  40.         recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,Locale.getDefault());
  41.         recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, activity.getPackageName());
  42.         recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
  43.         recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
  44.         recognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, MINIMUM_LENGTH_FOR_EXTRA_SPEECH_IN_MILLIS);
  45.         setListening(false);
  46.     }
  47.  
  48.     @Override
  49.     public void onReadyForSpeech(Bundle params) {
  50.  
  51.     }
  52.  
  53.     @Override
  54.     public void onBeginningOfSpeech() {
  55.         setListening(true);
  56.     }
  57.  
  58.     @Override
  59.     public void onRmsChanged(float rmsdB) {
  60.  
  61.     }
  62.  
  63.     @Override
  64.     public void onBufferReceived(byte[] buffer) {
  65.  
  66.     }
  67.  
  68.     @Override
  69.     public void onEndOfSpeech() {
  70.         setListening(false);
  71.     }
  72.  
  73.     @Override
  74.     public void onError(int error) {
  75.         mainActivity.setsubtitle(text);
  76.     }
  77.  
  78.     @Override
  79.     public void onResults(Bundle results) {
  80.  
  81.     }
  82.  
  83.     @Override
  84.     public void onPartialResults(Bundle partialResults) {
  85.         ArrayList<String> matches = partialResults
  86.                 .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
  87.         text = "";
  88.         if(matches!=null)
  89.             for (String result : matches)
  90.                 text += result + "\n";
  91.         mainActivity.setsubtitle(text);
  92.         setListening(false);
  93.     }
  94.  
  95.     @Override
  96.     public void onEvent(int eventType, Bundle params) {
  97.  
  98.     }
  99.  
  100.     public void callBackgroundListener(){
  101.         backgroundListener.run();
  102.     }
  103.  
  104.     public class backgroundVoiceListener extends Thread{
  105.         public void run(){
  106.             try {
  107.                 this.sleep(2000);
  108.                 if(!isListening()){
  109.                     setListening(true);
  110.                     speech.startListening(recognizerIntent);
  111.                     Toast.makeText(mainActivity,"Pode Falar", Toast.LENGTH_SHORT).show();
  112.                 }
  113.             } catch (InterruptedException e) {
  114.                 e.printStackTrace();
  115.             }
  116.         }
  117.     }
  118. }
Advertisement
RAW Paste Data Copied
Advertisement