Example usage for android.speech RecognizerIntent ACTION_RECOGNIZE_SPEECH

List of usage examples for android.speech RecognizerIntent ACTION_RECOGNIZE_SPEECH

Introduction

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

Prototype

String ACTION_RECOGNIZE_SPEECH

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

Click Source Link

Document

Starts an activity that will prompt the user for speech and send it through a speech recognizer.

Usage

From source file:com.theultimatelabs.scale.ScaleActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scale);/*from   w  w  w.j a v  a 2 s  .co  m*/

    Log.v(TAG, "onCreate");

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    mSettings = getSharedPreferences(PREFS, 0);

    mUnitsText = mSettings.getString("unitsText", "grams");
    mUnitsRatio = mSettings.getFloat("unitsRatio", (float) 1.0);

    mTts = new TextToSpeech(this, this);

    mUnitsView = (TextView) findViewById(R.id.text_unit);
    mUnitsView.setText(mUnitsText);

    findViewById(R.id.text_unit).setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            while (mTts.isSpeaking())
                ;
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say Units");
            startActivityForResult(intent, 0);

            if (adView != null) {
                adView.loadAd(new AdRequest());
            }

        }
    });

    mWeightTextView = (TextView) findViewById(R.id.text_weight);
    mWeightTextView.setText("00.00");
    /*
     * TextPaint weightTextPaint = mWeightTextView.getPaint(); CharSequence
     * weightText = mWeightTextView.getText(); while (weightText !=
     * TextUtils.ellipsize(weightText, weightTextPaint,
     * getWindowManager().getDefaultDisplay
     * ().getWidth()*2/3,TextUtils.TruncateAt.END)) {
     * weightTextPaint.setTextSize(weightTextPaint.getTextSize() - 1); }
     */

    mWeightTextView.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Zero'd", Toast.LENGTH_LONG).show();
            mZeroGrams = mWeightGrams;
            if (adView != null) {
                adView.loadAd(new AdRequest());
            }
        }
    });
    mWeightTextView.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            mZeroGrams = 0;
            Toast.makeText(getApplicationContext(), "Reset", Toast.LENGTH_LONG).show();
            if (adView != null) {
                adView.loadAd(new AdRequest());
            }
            return true;
        }
    });

    disableAdsText = (TextView) findViewById((R.id.text_disableAds));
    disableAdsText.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            new AlertDialog.Builder(ScaleActivity.this).setTitle("Keep Software Free and Open Source")
                    .setMessage("Ads help support further development, but they are OPTIONAL."
                            + " If you choose to disable ads, please consider donating. All dontations"
                            + " go towards purchasing hardware for open source development. "
                            + "Disabling ads or donating will not change the features availble in this app."
                            + " Thank you. rob@theultimatelabs.com")
                    .setPositiveButton("Disable Ads", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            adLayout.removeAllViews();
                            adView.removeAllViews();
                            disableAdsText.setVisibility(View.INVISIBLE);
                            mSettings.edit().putBoolean("ads", false).commit();
                            adView = null;
                        }
                    }).setCancelable(true).setNegativeButton("Keep Ads", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    }).setNeutralButton("Disable Ads + Donate", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            adLayout.removeAllViews();
                            adView.removeAllViews();
                            disableAdsText.setVisibility(View.INVISIBLE);
                            mSettings.edit().putBoolean("ads", false).commit();
                            adView = null;
                            startActivity(new Intent(Intent.ACTION_VIEW,
                                    Uri.parse("http://blog.theultimatelabs.com/p/donate.html")));
                        }
                    }).show();
        }
    });

    TextView aboutText = (TextView) findViewById((R.id.text_about));
    aboutText.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(getApplicationContext(), AboutActivity.class));
        }
    });

    /*
     * .setMessage() new AlertDialog.Builder(this) .setMessage(mymessage)
     * .setTitle(title) .setCancelable(true)
     * .setNeutralButton(android.R.string.cancel, new
     * DialogInterface.OnClickListener() { public void
     * onClick(DialogInterface dialog, int whichButton){} }) .show(); }}
     */
    // /

    mDensitiesJson = loadJsonResource(R.raw.densities);
    mVolumesJson = loadJsonResource(R.raw.volumes);
    mWeightsJson = loadJsonResource(R.raw.weights);

    // Initiate a generic request to load it with an ad
    if (mSettings.getBoolean("ads", true)) {
        // Create the adViewj
        adView = new AdView(this, AdSize.SMART_BANNER, "a15089dfb39c5a8");

        // Log.w(TAG, new Integer(R.id.layout_ads).toString());
        adLayout = (LinearLayout) findViewById(R.id.layout_ads);

        // Add the adView to it
        adLayout.addView(adView, 0);
        disableAdsText.setVisibility(View.VISIBLE);
    } else {
        disableAdsText.setVisibility(View.INVISIBLE);
        adView = null;
    }

    Intent intent = getIntent();
    mDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

    findScale();

}

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

/**
 *
 * @param lang recognition language//from  w  w w  .ja v a  2s  .  c o  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  www. j a v a  2s .  c om*/
  * @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.urbtek.phonegap.SpeechRecognizer.java

/**
 * Fire an intent to start the speech recognition activity.
 * /*from  ww w. j a v a2s  .c  om*/
 * @param args Argument array with the following string args: [req code][number of matches][prompt string]
 */
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);
    if (maxMatches > 0)
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxMatches);
    if (!prompt.isEmpty())
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt);
    ctx.startActivityForResult(this, intent, reqCode);
}

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]
 *///w  ww .j  a  v a2s .  c o m
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:conversandroid.RichASR.java

/**
 * Creates the speech recognizer instance if it is available
 * *//*w  ww.  j  a v a2s  .co  m*/
public void initASR() {

    // find out whether speech recognition is supported
    List<ResolveInfo> intActivities = this.getPackageManager()
            .queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);

    //Speech recognition does not currently work on simulated devices
    if ("generic".equals(Build.BRAND.toLowerCase())) {
        Log.e(LOGTAG, "ASR is not supported on virtual devices");
    } else {
        if (intActivities.size() != 0) {
            myASR = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
            myASR.setRecognitionListener(this);
        }
    }

    Log.i(LOGTAG, "ASR initialized");
}

From source file:com.wordpress.httpstheredefiningproductions.phonefinder.recorder.java

@Override
public void onCreate() {
    super.onCreate();
    //get the things above linked up to actual things in the app
    v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
    mSpeechRecognizer.setRecognitionListener(new SpeechRecognitionListener());
    mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());

}

From source file:atlc.granadaaccessibilityranking.VoiceActivity.java

/**
 * Creates the speech recognizer and text-to-speech synthesizer instances
 * @see RecognitionListener.java/*from w w w.j av a2  s  . com*/
 * @param ctx context of the interaction
 * */
public void initSpeechInputOutput(Activity ctx) {

    this.ctx = ctx;
    PackageManager packManager = ctx.getPackageManager();

    setTTS();

    // Find out whether speech recognition is supported
    List<ResolveInfo> intActivities = packManager
            .queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
    if (intActivities.size() != 0 || "generic".equals(Build.BRAND.toLowerCase(Locale.US))) {
        myASR = SpeechRecognizer.createSpeechRecognizer(ctx);
        myASR.setRecognitionListener(this);
    } else
        myASR = null;
}

From source file:com.example.SpeechRecognizer.java

/**
 * Fire an intent to start the speech recognition activity.
 * /*from   www  .  j  a v a2 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 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);
    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);
}

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 {/*from   w  w w. j  a  v a2 s  . c om*/
        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);
    }
}