Example usage for android.speech RecognizerIntent EXTRA_MAX_RESULTS

List of usage examples for android.speech RecognizerIntent EXTRA_MAX_RESULTS

Introduction

In this page you can find the example usage for android.speech RecognizerIntent EXTRA_MAX_RESULTS.

Prototype

String EXTRA_MAX_RESULTS

To view the source code for android.speech RecognizerIntent EXTRA_MAX_RESULTS.

Click Source Link

Document

Optional limit on the maximum number of results to return.

Usage

From source file:conversandroid.RichASR.java

/**
 * Starts speech recognition after checking the ASR parameters
 *
 * @param language Language used for speech recognition (e.g. Locale.ENGLISH)
 * @param languageModel Type of language model used (free form or web search)
 * @param maxResults Maximum number of recognition results
 *///from   ww w .j a  v  a 2 s  .c  o  m
public void listen(final Locale language, final String languageModel, final int maxResults) {
    Button b = (Button) findViewById(R.id.speech_btn);
    b.setEnabled(false);

    if ((languageModel.equals(RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
            || languageModel.equals(RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH)) && (maxResults >= 0)) {
        // Check we have permission to record audio
        checkASRPermission();

        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

        // Specify the calling package to identify the application
        intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName());
        //Caution: be careful not to use: getClass().getPackage().getName());

        // Specify language model
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, languageModel);

        // Specify how many results to receive. Results listed in order of confidence
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxResults);

        // Specify recognition language
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);

        Log.i(LOGTAG, "Going to start listening...");
        this.startListeningTime = System.currentTimeMillis();
        myASR.startListening(intent);

    } else {
        Log.e(LOGTAG, "Invalid params to listen method");
        ((TextView) findViewById(R.id.feedbackTxt)).setText("Error: invalid parameters");
    }

}

From source file:com.surveyorexpert.TalkToMe.java

private void sendRecognizeIntent() {
    // Intent //from  w  ww.  jav a2 s  .c o  m
    intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Press when complete");
    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 100);
    startActivityForResult(intent, SPEECH_REQUEST_CODE);
    //   Toast.makeText(getBaseContext(),"TalkToMe Done" , Toast.LENGTH_LONG).show();      
}

From source file:org.apache.cordova.nodialogspeechrecognizer.SpeechRecognizer.java

/**
 * Fire an intent to start the speech recognition activity.
 *
 * @param args//w  ww. ja va2  s.co  m
 *          Argument array with the following string args: [req code][number
 *          of matches][prompt string]
 */
private void startSpeechRecognitionActivity(JSONArray args) {
    int maxMatches = 1;
    String prompt = "";
    String language = Locale.getDefault().toString();

    try {
        if (args.length() > 0) {
            // Maximum number of matches, 0 means the recognizer decides
            String temp = args.getString(0);
            maxMatches = Integer.parseInt(temp);
        }
        if (args.length() > 1) {
            // Optional text prompt
            prompt = args.getString(1);
        }
        if (args.length() > 2) {
            // Optional language specified
            language = args.getString(2);
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, String.format("startSpeechRecognitionActivity exception: %s", e.toString()));
    }

    // Create the intent and set parameters
    final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, cordova.getActivity().getPackageName());
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);

    if (maxMatches > 0)
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxMatches);
    if (!prompt.equals(""))
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt);
    try {
        Handler loopHandler = new Handler(Looper.getMainLooper());
        loopHandler.post(new Runnable() {

            @Override
            public void run() {
                speech.startListening(intent);
            }

        });
    } catch (Exception e) {
        Log.e("error", "er", e);
    }
    // cordova.startActivityForResult(this, intent, REQUEST_CODE);
}

From source file:com.glabs.homegenie.util.VoiceControl.java

public void startListen() {
    _recognizer = getSpeechRecognizer();
    _recognizer.setRecognitionListener(this);
    ///*  w w  w .  java 2s .  c o m*/
    //speech recognition is supported - detect user button clicks
    //start the speech recognition intent passing required data
    Intent recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    //indicate package
    //recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
    //message to display while listening
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Your wish is my command!");
    //set speech model
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    //specify number of results to retrieve
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
    //start listening
    //startActivityForResult(listenIntent, VR_REQUEST);
    //startActivityForResult(recognizerIntent, VR_REQUEST);
    _recognizer.startListening(recognizerIntent);
}

From source file:com.phonegap.plugins.speech.XSpeechRecognizer.java

/**
 * Fire an intent to start the speech recognition activity.
 *
 * @param args Argument array with the following string args: [req code][number of matches]
 *///from  ww w  . ja v  a  2 s .c  o m
private void startSpeechRecognitionActivity(JSONArray args) {

    int maxMatches = 0;
    String language = Locale.getDefault().toString();

    try {
        if (args.length() > 0) {
            // Maximum number of matches, 0 means the recognizer decides
            String temp = args.getString(0);
            maxMatches = Integer.parseInt(temp);
        }
        if (args.length() > 1) {
            // Language
            language = args.getString(1);
        }
    } catch (Exception e) {
        Log.e(TAG, String.format("startSpeechRecognitionActivity exception: %s", e.toString()));
    }

    // Create the intent and set parameters
    final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    // intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);

    if (maxMatches > 0)
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxMatches);

    Handler loopHandler = new Handler(Looper.getMainLooper());
    loopHandler.post(new Runnable() {

        @Override
        public void run() {
            recognizer.startListening(intent);
        }

    });
}

From source file:com.todoroo.astrid.voice.VoiceInputAssistant.java

/**
 * Fire an intent to start the speech recognition activity.
 * This is fired by the listener on the microphone-button.
 *
 * @param prompt Specify the R.string.string_id resource for the prompt-text during voice-recognition here
 *//*from w w w. j  av  a 2 s .c  om*/
public void startVoiceRecognitionActivity(Fragment fragment, int prompt) {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, languageModel);
    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, ContextManager.getContext().getString(prompt));
    String detailMessage = "Error! No Fragment or Activity was registered to handle this voiceinput-request!";
    if (activity != null)
        activity.startActivityForResult(intent, requestCode);
    else if (fragment != null)
        fragment.startActivityForResult(intent, requestCode);
    else
        Log.e("Astrid VoiceInputAssistant", detailMessage, new IllegalStateException(detailMessage));
}

From source file:com.annuletconsulting.homecommand.node.MainFragment.java

protected void startRecognizing() {
    isListening = false;//from  w w  w  . ja  va2 s . c  om
    button.setBackgroundResource(R.drawable.listening);
    Intent recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3); //3 seems to be the minimum. when I set to 1 I get 5.
    speechRecognizer.startListening(recognizerIntent);
}

From source file:com.ct.speech.HintReceiver.java

/**
 * Fire an intent to start the speech recognition activity.
 * /* w  ww. j  a  va  2  s  . c  om*/
 * @param args
 *            Argument array with the following string args: [req
 *            code][number of matches][prompt string] Google speech
 *            recognizer
 */

private void startSpeechRecognitionActivity(JSONArray args) {
    // int reqCode = 42; // Hitchhiker? // global now
    int maxMatches = 2;
    String prompt = "";
    String language = "";
    try {
        if (args.length() > 0) {
            // Request code - passed back to the caller on a successful
            // operation
            String temp = args.getString(0);
            reqCode = Integer.parseInt(temp);
        }
        if (args.length() > 1) {
            // Maximum number of matches, 0 means the recognizer decides
            String temp = args.getString(1);
            maxMatches = Integer.parseInt(temp);
        }
        if (args.length() > 2) {
            // Optional text prompt
            prompt = args.getString(2);
        }
        if (args.length() > 3) {
            // Optional language specified
            language = args.getString(3);
        }
    } catch (Exception e) {
        Log.e(TAG, String.format("startSpeechRecognitionActivity exception: %s", e.toString()));
    }
    final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra("calling_package", "com.ct.BasicAppFrame");
    // If specific language
    if (!language.equals("")) {
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
    }
    if (maxMatches > 0)
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxMatches);
    if (!(prompt.length() == 0))
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt);
    // ctx.startActivityForResult(this, intent, reqCode); //removed to try
    // using recognizer directly
    try {
        this.ctx.runOnUiThread(new Runnable() {
            public void run() {
                final SpeechRecognizer recognizer = SpeechRecognizer.createSpeechRecognizer((Context) ctx);
                RecognitionListener listener = new RecognitionListener() {
                    @Override
                    public void onResults(Bundle results) {
                        //closeRecordedFile();
                        sendBackResults(results);
                        ArrayList<String> voiceResults = results
                                .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                        if (voiceResults == null) {
                            Log.e(TAG, "No voice results");
                        } else {
                            // Log.d(TAG, "Printing matches: ");
                            for (@SuppressWarnings("unused")
                            String match : voiceResults) {
                                // Log.d(TAG, match);
                            }
                        }
                        recognizer.destroy();
                    }

                    @Override
                    public void onReadyForSpeech(Bundle params) {
                        // Log.d(TAG, "Ready for speech");
                    }

                    @Override
                    public void onError(int error) {
                        Log.d(TAG, "Error listening for speech: " + error);
                        if (error == SpeechRecognizer.ERROR_NO_MATCH) {
                            sendBackResults(NO_MATCH);
                        } else if (error == SpeechRecognizer.ERROR_SPEECH_TIMEOUT) {
                            sendBackResults(NO_INPUT);
                        } else {
                            speechFailure("unknown error");
                        }
                        recognizer.destroy();
                    }

                    @Override
                    public void onBeginningOfSpeech() {
                        // Log.d(TAG, "Speech starting");
                        setStartOfSpeech();
                    }

                    @Override
                    //doesn't fire in Android after Ice Cream Sandwich
                    public void onBufferReceived(byte[] buffer) {
                    }

                    @Override
                    public void onEndOfSpeech() {
                        setEndOfSpeech();
                    }

                    @Override
                    public void onEvent(int eventType, Bundle params) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onPartialResults(Bundle partialResults) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onRmsChanged(float rmsdB) {
                        // TODO Auto-generated method stub

                    }
                };
                recognizer.setRecognitionListener(listener);
                Log.d(TAG, "starting speech recognition activity");
                recognizer.startListening(intent);
            }
        });
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:atlc.granadaaccessibilityranking.VoiceActivity.java

/**
 * Starts speech recognition after checking the ASR parameters
 *
 * @param language Language used for speech recognition (e.g. Locale.ENGLISH)
 * @param languageModel Type of language model used (free form or web search)
 * @param maxResults Maximum number of recognition results
 * @exception An exception is raised if the language specified is not available or the other parameters are not valid
 *//*w  w w  .  jav  a 2  s .  c o m*/
public void listen(final Locale language, final String languageModel, final int maxResults) throws Exception {
    checkASRPermission();

    if ((languageModel.equals(RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
            || languageModel.equals(RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH)) && (maxResults >= 0)) {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

        // Specify the calling package to identify the application
        intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, ctx.getPackageName());
        //Caution: be careful not to use: getClass().getPackage().getName());

        // Specify language model
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, languageModel);

        // Specify how many results to receive. Results listed in order of confidence
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxResults);

        // Specify recognition language
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);

        myASR.startListening(intent);

    } else {
        Log.e(LOGTAG, "Invalid params to listen method");
        throw new Exception("Invalid params to listen method"); //If the input parameters are not valid, it throws an exception
    }

}

From source file:com.telepromptu.TeleprompterService.java

private void startListening() {
    if (speechRecognizer == null) {
        speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
        speechRecognizer.setRecognitionListener(new DictationListener());
        Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
        speechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
        //          speechIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS,true);
        //          speechIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, 300000);
        //          speechIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 300000);
        speechRecognizer.startListening(speechIntent);
    }//from   w  w w  . j a  va 2s. c o m
}