Java Code Examples for android.speech.RecognizerIntent#ACTION_RECOGNIZE_SPEECH

The following examples show how to use android.speech.RecognizerIntent#ACTION_RECOGNIZE_SPEECH . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpeechRecognition.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private SpeechRecognition(final Context context, int nativeSpeechRecognizerImplAndroid) {
    mContext = context;
    mContinuous = false;
    mNativeSpeechRecognizerImplAndroid = nativeSpeechRecognizerImplAndroid;
    mListener = new Listener();
    mIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

    if (mRecognitionProvider != null) {
        mRecognizer = SpeechRecognizer.createSpeechRecognizer(mContext, mRecognitionProvider);
    } else {
        // It is possible to force-enable the speech recognition web platform feature (using a
        // command-line flag) even if initialize() failed to find the PROVIDER_PACKAGE_NAME
        // provider, in which case the first available speech recognition provider is used.
        // Caveat: Continuous mode may not work as expected with a different provider.
        mRecognizer = SpeechRecognizer.createSpeechRecognizer(mContext);
    }

    mRecognizer.setRecognitionListener(mListener);
}
 
Example 2
Source File: MainActivity.java    From QuickerAndroid with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 开启语音输入
 */
private void startVoiceInput() {
    //开启语音识别功能
    Intent intent = new Intent(
            RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    //设置模式,目前设置的是自由识别模式
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    //提示语言开始文字,就是效果图上面的文字
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please start your voice");
    //开始识别,这里检测手机是否支持语音识别并且捕获异常
    try {
        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);

    } catch (ActivityNotFoundException a) {
        Toast t = Toast.makeText(getApplicationContext(),
                "抱歉,您的设备当前不支持此功能。请安装Google语音搜索。",
                Toast.LENGTH_SHORT);
        t.show();
    }
}
 
Example 3
Source File: Home.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Showing google speech input dialog
 */
private synchronized void promptSpeechInput() {

    if (JoH.ratelimit("speech-input", 1)) {
        if (recognitionRunning) return;
        recognitionRunning = true;

        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_LANGUAGE, "en-US"); // debug voice
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                getString(R.string.speak_your_treatment));

        try {
            startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
        } catch (ActivityNotFoundException a) {
            Toast.makeText(getApplicationContext(),
                    R.string.speech_recognition_is_not_supported,
                    Toast.LENGTH_LONG).show();
        }
    }
}
 
Example 4
Source File: SearchBox.java    From WeGit with Apache License 2.0 6 votes vote down vote up
/***
 * Start the voice input activity manually
 */
public void startVoiceRecognition() {
	if (isMicEnabled()) {
		Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
				RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
		intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
				context.getString(R.string.speak_now));
		if (mContainerActivity != null) {
			mContainerActivity.startActivityForResult(intent, VOICE_RECOGNITION_CODE);
		} else if (mContainerFragment != null) {
			mContainerFragment.startActivityForResult(intent, VOICE_RECOGNITION_CODE);
		} else if (mContainerSupportFragment != null) {
			mContainerSupportFragment.startActivityForResult(intent, VOICE_RECOGNITION_CODE);
		}
	}
}
 
Example 5
Source File: DroidSpeech.java    From DroidSpeech with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes the droid speech properties
 */
private void initDroidSpeechProperties()
{
    // Initializing the droid speech recognizer
    droidSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(context);

    // Initializing the speech 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, context.getPackageName());
    speechIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
    speechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, Extensions.MAX_VOICE_RESULTS);
    if(dsProperties.currentSpeechLanguage != null)
    {
        // Setting the speech language
        speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, dsProperties.currentSpeechLanguage);
        speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, dsProperties.currentSpeechLanguage);
    }

    if(dsProperties.offlineSpeechRecognition && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    {
        // Setting offline speech recognition to true
        speechIntent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true);
    }

    // Initializing the audio Manager
    audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
}
 
Example 6
Source File: DefaultVoiceRecognizerDelegate.java    From PersistentSearchView with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isVoiceRecognitionAvailable() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    PackageManager mgr = getContext().getPackageManager();
    if (mgr != null) {
        List<ResolveInfo> list = mgr.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
    }
    return false;
}
 
Example 7
Source File: SearchView.java    From Material-SearchView with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCloseVoiceClicked() {
    if (mSearchEditText != null && mSearchEditText.getText() != null && mSearchEditText.getText().toString().length() != 0) {
        mSearchEditText.getText().clear();
        mQuery = "";
    } else if (isSpeechRecognitionToolAvailable()) {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        startActivityForResult(intent, RECOGNIZER_CODE);
    }
}
 
Example 8
Source File: NiboDefaultVoiceRecognizerDelegate.java    From Nibo with MIT License 5 votes vote down vote up
@Override
public boolean isVoiceRecognitionAvailable() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    PackageManager mgr = getContext().getPackageManager();
    if (mgr != null) {
        List<ResolveInfo> list = mgr.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
    }
    return false;
}
 
Example 9
Source File: MainActivity.java    From SimplicityBrowser with MIT License 5 votes vote down vote up
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.action_search));
    try {
        startActivityForResult(intent, 22);
    } catch (ActivityNotFoundException a) {
        Cardbar.snackBar(getApplicationContext(), getString(R.string.error), true).show();

    }
}
 
Example 10
Source File: SearchLiveo.java    From searchliveo with Apache License 2.0 5 votes vote down vote up
private void startVoice() {
    hideKeybord();

    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, mContext.getString(R.string.liveo_search_view_voice));

    try {
        mContext.startActivityForResult(intent, REQUEST_CODE_SPEECH_INPUT);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(mContext.getApplicationContext(), R.string.liveo_not_supported, Toast.LENGTH_SHORT).show();
    }
}
 
Example 11
Source File: DefaultVoiceRecognizerDelegate.java    From PersistentSearchView with Apache License 2.0 5 votes vote down vote up
@Override
public Intent buildVoiceRecognitionIntent() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
            getContext().getString(R.string.speak_now));
    return intent;
}
 
Example 12
Source File: Audio.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Fire an intent to start the speech recognition activity. onActivityResult
 * is handled in BaseActivity
 */
private void startVoiceRecognitionActivity(Activity a) {

    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Tell me something!");
    a.startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}
 
Example 13
Source File: PackageUtils.java    From FloatingSearchView with Apache License 2.0 5 votes vote down vote up
static public void startTextToSpeech(Activity context, String prompt, int requestCode) {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt);
    try {
        context.startActivityForResult(intent, requestCode);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(context, context.getString(R.string.speech_not_supported),
                Toast.LENGTH_SHORT).show();
    }
}
 
Example 14
Source File: MainActivity.java    From io2014-codelabs with Apache License 2.0 5 votes vote down vote up
public void voiceSearch(View view) {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    // Specify the calling package to identify your application
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());

    // Display an hint to the user about what he should say.
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.string.try_play_dual_core));

    // Given an hint to the recognizer about what the user is going to say
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

    startActivityForResult(intent, RECOGNIZER_REQ_CODE);
}
 
Example 15
Source File: MicConfiguration.java    From BotLibre with Eclipse Public License 1.0 5 votes vote down vote up
public void googleListening(View v){
	txt.setText("Status: ON");
	setMicIcon(true, false);
	Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
	intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, MainActivity.voice.language);
	try {
		startActivityForResult(intent, 1);
		editTextForGoogle.setText("");
	} catch (ActivityNotFoundException a) {
		Toast t = Toast.makeText(getApplicationContext(),
				"Your device doesn't support Speech to Text",
				Toast.LENGTH_SHORT);
		t.show();
	}
}
 
Example 16
Source File: LocationBarLayout.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
/**
 * Triggers a voice recognition intent to allow the user to specify a search query.
 */
@Override
public void startVoiceRecognition() {
    Activity activity = mWindowAndroid.getActivity().get();
    if (activity == null) return;

    if (!mWindowAndroid.hasPermission(Manifest.permission.RECORD_AUDIO)) {
        if (mWindowAndroid.canRequestPermission(Manifest.permission.RECORD_AUDIO)) {
            WindowAndroid.PermissionCallback callback =
                    new WindowAndroid.PermissionCallback() {
                @Override
                public void onRequestPermissionsResult(
                        String[] permissions, int[] grantResults) {
                    if (grantResults.length != 1) return;

                    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        startVoiceRecognition();
                    } else {
                        updateMicButtonState();
                    }
                }
            };
            mWindowAndroid.requestPermissions(
                    new String[] {Manifest.permission.RECORD_AUDIO}, callback);
        } else {
            updateMicButtonState();
        }
        return;
    }

    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
            activity.getComponentName().flattenToString());
    intent.putExtra(RecognizerIntent.EXTRA_WEB_SEARCH_ONLY, true);

    if (mWindowAndroid.showCancelableIntent(intent, this, R.string.voice_search_error) < 0) {
        // Requery whether or not the recognition intent can be handled.
        FeatureUtilities.isRecognitionIntentPresent(activity, false);
        updateMicButtonState();
    }
}
 
Example 17
Source File: GoogleImeSpeechRecognition.java    From Android-Speech-Recognition with MIT License 4 votes vote down vote up
void initialize(){
    recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, SpeechRecognition.MAX_RESULT_COUNT);
}
 
Example 18
Source File: LocationBarLayout.java    From 365browser with Apache License 2.0 4 votes vote down vote up
/**
 * Triggers a voice recognition intent to allow the user to specify a search query.
 */
@Override
public void startVoiceRecognition() {
    Activity activity = mWindowAndroid.getActivity().get();
    if (activity == null) return;

    if (!mWindowAndroid.hasPermission(Manifest.permission.RECORD_AUDIO)) {
        if (mWindowAndroid.canRequestPermission(Manifest.permission.RECORD_AUDIO)) {
            WindowAndroid.PermissionCallback callback =
                    new WindowAndroid.PermissionCallback() {
                @Override
                public void onRequestPermissionsResult(
                        String[] permissions, int[] grantResults) {
                    if (grantResults.length != 1) return;

                    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        startVoiceRecognition();
                    } else {
                        updateMicButtonState();
                    }
                }
            };
            mWindowAndroid.requestPermissions(
                    new String[] {Manifest.permission.RECORD_AUDIO}, callback);
        } else {
            updateMicButtonState();
        }
        return;
    }

    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
            activity.getComponentName().flattenToString());
    intent.putExtra(RecognizerIntent.EXTRA_WEB_SEARCH_ONLY, true);

    if (mWindowAndroid.showCancelableIntent(intent, this, R.string.voice_search_error) < 0) {
        // Requery whether or not the recognition intent can be handled.
        FeatureUtilities.isRecognitionIntentPresent(activity, false);
        updateMicButtonState();
    }
}
 
Example 19
Source File: MicConfiguration.java    From BotLibre with Eclipse Public License 1.0 4 votes vote down vote up
@TargetApi(23)
private void beginListening() {
	setStreamVolume();
	lastReply = System.currentTimeMillis();
	
	muteMicBeep(true);
	
	Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
	if (MainActivity.offlineSpeech) {
		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, MainActivity.voice.language);

		if (!this.failedOfflineLanguage) {
			//en-US will use the English in offline.
			intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
			// intent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true);
		}
		intent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true);
	} else {
		if (MainActivity.voice.language != null && !MainActivity.voice.language.isEmpty()) {
			intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, MainActivity.voice.language);
			if (!this.failedOfflineLanguage) {
				intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, MainActivity.voice.language);
			}
		} else {
			intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en");
			if (!this.failedOfflineLanguage) {
				intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en");
			}
		}
	}

	try {
		Log.d("BeginListening","StartListening");
		this.speech.startListening(intent);
		setMicIcon(true, false);
	} catch (ActivityNotFoundException a) {
		Log.d("BeginListening","CatchError: " + a.getMessage());
		Toast t = Toast.makeText(getApplicationContext(),
				"Your device doesn't support Speech to Text",
				Toast.LENGTH_SHORT);
		t.show();
		txt.setText("Status: Your device doesn't support Speech to text.");
	}		
}
 
Example 20
Source File: SearchFragment.java    From adt-leanback-support with Apache License 2.0 3 votes vote down vote up
/**
 * Returns an intent that can be used to request speech recognition.
 * Built from the base {@link RecognizerIntent#ACTION_RECOGNIZE_SPEECH} plus
 * extras:
 *
 * <ul>
 * <li>{@link RecognizerIntent#EXTRA_LANGUAGE_MODEL} set to
 * {@link RecognizerIntent#LANGUAGE_MODEL_FREE_FORM}</li>
 * <li>{@link RecognizerIntent#EXTRA_PARTIAL_RESULTS} set to true</li>
 * <li>{@link RecognizerIntent#EXTRA_PROMPT} set to the search bar hint text</li>
 * </ul>
 *
 * For handling the intent returned from the service, see
 * {@link #setSearchQuery(Intent, boolean)}.
 */
public Intent getRecognizerIntent() {
    Intent recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
    if (mSearchBar != null && mSearchBar.getHint() != null) {
        recognizerIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, mSearchBar.getHint());
    }
    return recognizerIntent;
}