Skip to content Skip to sidebar Skip to footer

Is It Possible To Use Android's Speech Recognition Without Showing The Dialog?

I want to use the speech to text api in android, but I don't want to be slowed down by those dialogs. Is it possible to use the functionality without them?

Solution 1:

Yes it is possible:

You can use the SpeechRecognizer class from the API. Make sure it is used in the main thread - as required according to the API - and that your class implements the RecognitionListener.

privateSpeechRecognizer speech;

@OverridepublicvoidonCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    speech = SpeechRecognizer.createSpeechRecognizer(this);
    speech.setRecognitionListener(this);

    Intent intent = newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
        this.getPackageName());

    speech.startListening(intent);
}

@OverridepublicvoidonBeginningOfSpeech()
{
}

@OverridepublicvoidonBufferReceived(byte[] arg0)
{
}

@OverridepublicvoidonEndOfSpeech()
{
}

@OverridepublicvoidonError(int e)
{
}

@OverridepublicvoidonEvent(int arg0, Bundle arg1)
{
}

@OverridepublicvoidonPartialResults(Bundle arg0)
{
}

@OverridepublicvoidonReadyForSpeech(Bundle arg0)
{
}

@OverridepublicvoidonResults(Bundle data)
{
    ArrayList<String> matches = data.getStringArrayList(
        SpeechRecognizer.RESULTS_RECOGNITION);

    // Do something useful with the matches =)// ...
}

@OverridepublicvoidonRmsChanged(float arg0)
{
}

Solution 2:

try this code

import java.util.ArrayList;

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 android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ToggleButton;

publicclassVoiceRecognitionActivityextendsActivityimplementsRecognitionListener {

    privateTextView returnedText;
    privateToggleButton toggleButton;
    privateProgressBar progressBar;
    privateSpeechRecognizer speech = null;
    privateIntent recognizerIntent;
    privateStringLOG_TAG = "VoiceRecognitionActivity";

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_voice_recognition);
        returnedText = (TextView) findViewById(R.id.textView1);
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);

        progressBar.setVisibility(View.INVISIBLE);
        speech = SpeechRecognizer.createSpeechRecognizer(this);
        speech.setRecognitionListener(this);
        recognizerIntent = newIntent(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, 3);

        toggleButton.setOnCheckedChangeListener(newOnCheckedChangeListener() {

            @OverridepublicvoidonCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if (isChecked) {
                    progressBar.setVisibility(View.VISIBLE);
                    progressBar.setIndeterminate(true);
                    speech.startListening(recognizerIntent);
                } else {
                    progressBar.setIndeterminate(false);
                    progressBar.setVisibility(View.INVISIBLE);
                    speech.stopListening();
                }
            }
        });

    }

    @OverridepublicvoidonResume() {
        super.onResume();
    }

    @OverrideprotectedvoidonPause() {
        super.onPause();
        if (speech != null) {
            speech.destroy();
            Log.i(LOG_TAG, "destroy");
        }

    }

    @OverridepublicvoidonBeginningOfSpeech() {
        Log.i(LOG_TAG, "onBeginningOfSpeech");
        progressBar.setIndeterminate(false);
        progressBar.setMax(10);
    }

    @OverridepublicvoidonBufferReceived(byte[] buffer) {
        Log.i(LOG_TAG, "onBufferReceived: " + buffer);
    }

    @OverridepublicvoidonEndOfSpeech() {
        Log.i(LOG_TAG, "onEndOfSpeech");
        progressBar.setIndeterminate(true);
        toggleButton.setChecked(false);
    }

    @OverridepublicvoidonError(int errorCode) {
        String errorMessage = getErrorText(errorCode);
        Log.d(LOG_TAG, "FAILED " + errorMessage);
        returnedText.setText(errorMessage);
        toggleButton.setChecked(false);
    }

    @OverridepublicvoidonEvent(int arg0, Bundle arg1) {
        Log.i(LOG_TAG, "onEvent");
    }

    @OverridepublicvoidonPartialResults(Bundle arg0) {
        Log.i(LOG_TAG, "onPartialResults");
    }

    @OverridepublicvoidonReadyForSpeech(Bundle arg0) {
        Log.i(LOG_TAG, "onReadyForSpeech");
    }

    @OverridepublicvoidonResults(Bundle results) {
        Log.i(LOG_TAG, "onResults");
        ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        String text = "";
        for (String result : matches)
            text += result + "\n";

        returnedText.setText(text);
    }

    @OverridepublicvoidonRmsChanged(float rmsdB) {
        Log.i(LOG_TAG, "onRmsChanged: " + rmsdB);
        progressBar.setProgress((int) rmsdB);
    }

    publicstaticStringgetErrorText(int errorCode) {
        String message;
        switch (errorCode) {
        caseSpeechRecognizer.ERROR_AUDIO:
            message = "Audio recording error";
            break;
        caseSpeechRecognizer.ERROR_CLIENT:
            message = "Client side error";
            break;
        caseSpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
            message = "Insufficient permissions";
            break;
        caseSpeechRecognizer.ERROR_NETWORK:
            message = "Network error";
            break;
        caseSpeechRecognizer.ERROR_NETWORK_TIMEOUT:
            message = "Network timeout";
            break;
        caseSpeechRecognizer.ERROR_NO_MATCH:
            message = "No match";
            break;
        caseSpeechRecognizer.ERROR_RECOGNIZER_BUSY:
            message = "RecognitionService busy";
            break;
        caseSpeechRecognizer.ERROR_SERVER:
            message = "error from server";
            break;
        caseSpeechRecognizer.ERROR_SPEECH_TIMEOUT:
            message = "No speech input";
            break;
        default:
            message = "Didn't understand, please try again.";
            break;
        }
        return message;
    }

}

activity_voice_recognition.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><ImageViewandroid:id="@+id/imageView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:src="@drawable/ic_launcher" /><ProgressBarandroid:id="@+id/progressBar1"style="?android:attr/progressBarStyleHorizontal"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_below="@+id/toggleButton1"android:layout_marginTop="28dp"android:paddingLeft="10dp"android:paddingRight="10dp" /><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/progressBar1"android:layout_centerHorizontal="true"android:layout_marginTop="47dp" /><ToggleButtonandroid:id="@+id/toggleButton1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_marginTop="26dp"android:text="ToggleButton" /></RelativeLayout>

AndroidManifest.xml

<manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.rakesh.voicerecognitionexample"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="15" /><uses-permissionandroid:name="android.permission.RECORD_AUDIO"/><uses-permissionandroid:name="android.permission.INTERNET" /><applicationandroid:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name=".VoiceRecognitionActivity"android:label="@string/title_activity_voice_recognition" ><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

Solution 3:

No.

The Intent for the speech recognition displays the dialog and there are no options to disable showing it.

You should also know that the speech recognition feature requires an internet connection so that the sound can be uploaded to Google for recognition.

Post a Comment for "Is It Possible To Use Android's Speech Recognition Without Showing The Dialog?"