Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.rafaelrp.melo_birthday;
- import android.Manifest;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.speech.RecognitionListener;
- import android.speech.RecognizerIntent;
- import android.speech.SpeechRecognizer;
- import android.support.v4.app.ActivityCompat;
- import android.widget.Toast;
- import java.util.ArrayList;
- import java.util.Locale;
- public class SpeechToText implements RecognitionListener {
- private SpeechRecognizer speech = null;
- private Intent recognizerIntent;
- private MainActivity mainActivity;
- private String text = "";
- private boolean isListening = false;
- private int MINIMUM_LENGTH_FOR_EXTRA_SPEECH_IN_MILLIS = 3000;
- private final backgroundVoiceListener backgroundListener;
- public boolean isListening() {
- return isListening;
- }
- public void setListening(boolean listening) {
- isListening = listening;
- }
- public SpeechToText(MainActivity activity) {
- mainActivity = activity;
- backgroundListener = new backgroundVoiceListener();
- speech = SpeechRecognizer.createSpeechRecognizer(activity);
- speech.setRecognitionListener(this);
- recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
- recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,Locale.getDefault());
- recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, activity.getPackageName());
- recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
- recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
- recognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, MINIMUM_LENGTH_FOR_EXTRA_SPEECH_IN_MILLIS);
- setListening(false);
- }
- @Override
- public void onReadyForSpeech(Bundle params) {
- }
- @Override
- public void onBeginningOfSpeech() {
- setListening(true);
- }
- @Override
- public void onRmsChanged(float rmsdB) {
- }
- @Override
- public void onBufferReceived(byte[] buffer) {
- }
- @Override
- public void onEndOfSpeech() {
- setListening(false);
- }
- @Override
- public void onError(int error) {
- mainActivity.setsubtitle(text);
- }
- @Override
- public void onResults(Bundle results) {
- }
- @Override
- public void onPartialResults(Bundle partialResults) {
- ArrayList<String> matches = partialResults
- .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
- text = "";
- if(matches!=null)
- for (String result : matches)
- text += result + "\n";
- mainActivity.setsubtitle(text);
- setListening(false);
- }
- @Override
- public void onEvent(int eventType, Bundle params) {
- }
- public void callBackgroundListener(){
- backgroundListener.run();
- }
- public class backgroundVoiceListener extends Thread{
- public void run(){
- try {
- this.sleep(2000);
- if(!isListening()){
- setListening(true);
- speech.startListening(recognizerIntent);
- Toast.makeText(mainActivity,"Pode Falar", Toast.LENGTH_SHORT).show();
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement