Example usage for android.speech RecognizerIntent EXTRA_LANGUAGE

List of usage examples for android.speech RecognizerIntent EXTRA_LANGUAGE

Introduction

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

Prototype

String EXTRA_LANGUAGE

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

Click Source Link

Document

Optional IETF language tag (as defined by BCP 47), for example "en-US".

Usage

From source file:com.hollowsoft.smarthome.view.MainScreen.java

public void onSpeak(final View view) {
    ////  ww w.  j  a va 2  s. c o m
    // final SpeechRecognizer recognizer =
    // SpeechRecognizer.createSpeechRecognizer(this);
    //
    // final Intent intent = new
    // Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    //
    // intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "pt-BR");
    //
    // recognizer.startListening(intent);
    //
    // recognizer.setRecognitionListener(this);
    final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "pt-BR");
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);

    startActivityForResult(intent, 1337);
}

From source file:com.vyasware.vaani.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    returnedText = (TextView) findViewById(R.id.textView1);
    outputText = (TextView) findViewById(R.id.textView2);
    progressBar = (ProgressBar) findViewById(R.id.progressBar1);
    toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);

    progressBar.setVisibility(View.INVISIBLE);
    speech = SpeechRecognizer.createSpeechRecognizer(this);
    speech.setRecognitionListener(this);
    recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    //        recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,
    //                "en");
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "hi-IN");
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "hi-IN");
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE, "hi-IN");
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
    tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override//from  w  w w . j a v a2 s. c  o  m
        public void onInit(int status) {
            if (status != TextToSpeech.ERROR) {
                tts.setLanguage(new Locale("hi_IN"));
                tts.setSpeechRate(0.9f);
            }
        }
    });
    returnedText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
            intent.putExtra(SearchManager.QUERY, returnedText.getText());
            if (intent.resolveActivity(getPackageManager()) != null)
                startActivity(intent);

        }
    });

    toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                progressBar.setVisibility(View.VISIBLE);
                progressBar.setIndeterminate(true);
                speech.startListening(recognizerIntent);
                outputText.setText("");
            } else {
                progressBar.setIndeterminate(false);
                progressBar.setVisibility(View.INVISIBLE);
                speech.stopListening();
            }
        }
    });

}

From source file:com.github.zagum.speechrecognitionview.sample.MainActivity.java

private void startRecognition() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName());
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en");
    speechRecognizer.startListening(intent);
}

From source file:ai.api.unityhelper.RecognitionHelper.java

/**
 *
 * @param lang recognition language//from ww w. j a va  2 s .co  m
 */
private void startListening(final String lang) {
    if (!recognitionActive) {

        final Intent sttIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        sttIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

        final String language = lang.replace('-', '_');

        sttIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
        sttIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, language);

        // WORKAROUND for https://code.google.com/p/android/issues/detail?id=75347
        // TODO Must be removed after fix in Android
        sttIntent.putExtra("android.speech.extra.EXTRA_ADDITIONAL_LANGUAGES", new String[] {});

        runInUiThread(new Runnable() {
            @Override
            public void run() {
                initializeRecognizer();

                speechRecognizer.startListening(sttIntent);
                recognitionActive = true;
            }
        });

    } else {
        Log.w(TAG, "Trying to start recognition while another recognition active");
    }
}

From source file:com.corumgaz.mobilsayac.VoiceRecognizer.VoiceRecognizer.java

/**
  * Fire an intent to start the speech recognition activity.
  */*from w ww .j  a  v a  2  s .c  o  m*/
  * @param args Argument array with the following string args: [req code][number of matches][prompt string]
  */
private void startSpeechRecognitionActivity(JSONArray args) {
    int maxMatches = 0;
    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
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

    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);
    cordova.startActivityForResult(this, intent, REQUEST_CODE);
}

From source file:com.ipo.wiimote.SpeechRecognizer.java

/**
 * Fire an intent to start the speech recognition activity.
 *
 * @param args Argument array with the following string args: [req code][number of matches][prompt string]
 *//*from   w  w w  . ja va2s .com*/
private void startSpeechRecognitionActivity(JSONArray args) {
    int reqCode = 42; //Hitchhiker?
    int maxMatches = 0;
    String prompt = "";

    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);
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, String.format("startSpeechRecognitionActivity exception: %s", e.toString()));
    }

    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.ENGLISH);
    if (maxMatches > 0)
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxMatches);
    if (!prompt.equals(""))
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt);
    cordova.startActivityForResult(this, intent, reqCode);
}

From source file:com.mobicage.rogerthat.plugins.messaging.widgets.TextLineWidget.java

@Override
public void initializeWidget() {
    mEditText = (EditText) findViewById(R.id.edit_text);
    if (mColorScheme == BrandingMgr.ColorScheme.DARK) {
        UIUtils.setColors(ContextCompat.getColor(mActivity, R.color.mc_white), mEditText);
    } else {//w w w .  ja va  2s .  c  o m
        UIUtils.setColors(mActivity, mEditText);
    }
    mEditText.setTextColor(mTextColor);
    mEditText.setText((String) mWidgetMap.get("value"));
    mEditText.setHint((String) mWidgetMap.get("place_holder"));
    mEditText.setFilters(new InputFilter[] {
            new InputFilter.LengthFilter(((Long) mWidgetMap.get("max_chars")).intValue()) });
    mEditText.setInputType(
            getDefaultInputTypes() | KeyboardType.getInputType((String) mWidgetMap.get("keyboard_type")));

    ImageButton btnSpeak = (ImageButton) findViewById(R.id.btn_speak);
    if (AppConstants.SPEECH_TO_TEXT && isSpeechRecognitionActivityPresented(mActivity)) {
        IconicsDrawable icon = new IconicsDrawable(mActivity, FontAwesome.Icon.faw_microphone)
                .color(LookAndFeelConstants.getPrimaryIconColor(mActivity)).sizeDp(20);
        btnSpeak.setVisibility(View.VISIBLE);
        btnSpeak.setImageDrawable(icon);
        btnSpeak.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                    voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                    voiceIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS,
                            1500);
                    voiceIntent.putExtra(
                            RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, 1500);
                    voiceIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 15000);
                    voiceIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
                    voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
                    mActivity.startActivityForResult(voiceIntent, REQUEST_CODE_VOICE);
                } catch (ActivityNotFoundException e) {
                    L.bug(e);
                }
            }
        });
    } else {
        btnSpeak.setVisibility(View.GONE);
    }
}

From source file:org.apache.cordova.plugins.speech.SpeechRecognizer.java

/**
 * Fire an intent to start the speech recognition activity.
 *
 * @param args Argument array with the following string args: [req code][number of matches][prompt string]
 *///from   w ww  . j  av  a 2s . c  om
private void startSpeechRecognitionActivity(JSONArray args) {
    int reqCode = 42; //Hitchhiker?
    int maxMatches = 0;
    String prompt = "";

    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);
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, String.format("startSpeechRecognitionActivity exception: %s", e.toString()));
    }

    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.ENGLISH);
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "org.apache.cordova.plugins.speech");

    if (maxMatches > 0)
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxMatches);
    if (!prompt.equals(""))
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt);

    Log.d(LOG_TAG, "startActivityForResult");
    cordova.startActivityForResult(this, intent, reqCode);
}

From source file:com.ola.insta.BookingAcivity.java

/**
 * Showing google speech input dialog//  w w  w.  j av  a  2  s. co m
 * */
private void promptSpeechInput() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.string.speech_prompt));
    try {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(getApplicationContext(), getString(R.string.speech_not_supported), Toast.LENGTH_SHORT)
                .show();
    }
}

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 w w w  .  ja v a2  s.c  om*/
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");
    }

}