Example usage for android.speech RecognizerIntent EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE

List of usage examples for android.speech RecognizerIntent EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE

Introduction

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

Prototype

String EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE

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

Click Source Link

Document

Specify this boolean extra in a broadcast of #ACTION_GET_LANGUAGE_DETAILS to indicate that only the current language preference is needed in the response.

Usage

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// ww w.j  a  va2s. co 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();
            }
        }
    });

}