Java Code Examples for android.speech.SpeechRecognizer#createSpeechRecognizer()

The following examples show how to use android.speech.SpeechRecognizer#createSpeechRecognizer() . 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: GoogleRecognitionServiceImpl.java    From dialogflow-android-client with Apache License 2.0 6 votes vote down vote up
protected void initializeRecognizer() {
    if (speechRecognizer != null) {
        return;
    }

    synchronized (speechRecognizerLock) {
        if (speechRecognizer != null) {
            speechRecognizer.destroy();
            speechRecognizer = null;
        }

        final ComponentName component = RecognizerChecker.findGoogleRecognizer(context);
        speechRecognizer = SpeechRecognizer.createSpeechRecognizer(context, component);
        speechRecognizer.setRecognitionListener(new InternalRecognitionListener());
    }
}
 
Example 3
Source File: SpeechRecognizerManager.java    From ContinuesVoiceRecognition with MIT License 6 votes vote down vote up
public SpeechRecognizerManager(Context context,onResultsReady listener)
{
    try{
        mListener=listener;
    }
    catch(ClassCastException e)
    {
        Log.e(TAG,e.toString());
    }
    mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(context);
    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,
            context.getPackageName());
    startListening();

}
 
Example 4
Source File: SpeechRecognition.java    From 365browser with Apache License 2.0 6 votes vote down vote up
private SpeechRecognition(long nativeSpeechRecognizerImplAndroid) {
    mContinuous = false;
    mNativeSpeechRecognizerImplAndroid = nativeSpeechRecognizerImplAndroid;
    mListener = new Listener();
    mIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

    if (sRecognitionProvider != null) {
        mRecognizer = SpeechRecognizer.createSpeechRecognizer(
                ContextUtils.getApplicationContext(), sRecognitionProvider);
    } 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(ContextUtils.getApplicationContext());
    }

    mRecognizer.setRecognitionListener(mListener);
}
 
Example 5
Source File: SearchFragment.java    From adt-leanback-support with Apache License 2.0 5 votes vote down vote up
@Override
public void onResume() {
    super.onResume();
    if (mSpeechRecognitionCallback == null && null == mSpeechRecognizer) {
        mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(getActivity());
        mSearchBar.setSpeechRecognizer(mSpeechRecognizer);
    }
}
 
Example 6
Source File: VoiceControl.java    From HomeGenie-Android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * lazy initialize the speech recognizer
 */
private SpeechRecognizer getSpeechRecognizer() {
    if (_recognizer == null) {
        _recognizer = SpeechRecognizer.createSpeechRecognizer(_hgcontext);
        _recognizer.setRecognitionListener(this);
    }
    return _recognizer;
}
 
Example 7
Source File: SearchSupportFragment.java    From adt-leanback-support with Apache License 2.0 5 votes vote down vote up
@Override
public void onResume() {
    super.onResume();
    if (mSpeechRecognitionCallback == null && null == mSpeechRecognizer) {
        mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(getActivity());
        mSearchBar.setSpeechRecognizer(mSpeechRecognizer);
    }
}
 
Example 8
Source File: SpeechRecognitionManager.java    From talkback with Apache License 2.0 5 votes vote down vote up
/** Creates a speech recognizer & checks if the user has voice recognition ability. */
private void createSpeechRecognizer() {
  // Checks if user can use voice recognition.
  speechRecognizer = SpeechRecognizer.createSpeechRecognizer(talkbackContext);
  if (!SpeechRecognizer.isRecognitionAvailable(talkbackContext)) {
    Toast.makeText(
            talkbackContext,
            talkbackContext.getString(R.string.voice_commands_no_voice_recognition_ability),
            Toast.LENGTH_SHORT)
        .show();
    return;
  }
}
 
Example 9
Source File: MainActivity.java    From iqra-android with MIT License 5 votes vote down vote up
protected void setupSpeechInput() {
    mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
    mSpeechRecognizer.setRecognitionListener(this);
    mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    if (!mSpeechRecognizerIntent.hasExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE)) {
        mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
    }
    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ar-AE");
    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
    mIsListening = false;
}
 
Example 10
Source File: Speech.java    From android-speech with Apache License 2.0 5 votes vote down vote up
private void initSpeechRecognizer(final Context context) {
    if (context == null)
        throw new IllegalArgumentException("context must be defined!");

    mContext = context;

    if (SpeechRecognizer.isRecognitionAvailable(context)) {
        if (mSpeechRecognizer != null) {
            try {
                mSpeechRecognizer.destroy();
            } catch (final Throwable exc) {
                Logger.debug(Speech.class.getSimpleName(),
                        "Non-Fatal error while destroying speech. " + exc.getMessage());
            } finally {
                mSpeechRecognizer = null;
            }
        }

        mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(context);
        mSpeechRecognizer.setRecognitionListener(mListener);
        initDelayedStopListening(context);

    } else {
        mSpeechRecognizer = null;
    }

    mPartialData.clear();
    mUnstableData = null;
}
 
Example 11
Source File: SpeechToTextManagerImpl.java    From mirror with Apache License 2.0 5 votes vote down vote up
@Override
public void start() {
    Timber.d("start");
    if (mIntent != null) {
        return;
    }

    initializeCommands();
    initializeIntent();
    initializeRecognitionListener();

    mRecognizer = SpeechRecognizer.createSpeechRecognizer(mAppManager.getAppContext());
    mRecognizer.setRecognitionListener(mRecognitionListener);
}
 
Example 12
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 13
Source File: ChatActivity.java    From BotLibre with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void onError(int error) {
	debug("onError:" + error);
	Log.d("onError Info", "ChatActivity on error executes here!");
	try{
	isRecording = false;

	lastReply = System.currentTimeMillis();
	this.speech.destroy();
	this.speech = SpeechRecognizer.createSpeechRecognizer(this);
	this.speech.setRecognitionListener(this);
	
	setMicIcon(false, false);

	muteMicBeep(false);

	setStreamVolume();
	
	if (error == SpeechRecognizer.ERROR_AUDIO) {
		Log.d("System.out", "Error: Audio Recording Error");
	} else if (error == SpeechRecognizer.ERROR_CLIENT) {
		Log.d("System.out", "Error: Other client side error");
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS) {
		Log.d("System.out", "Error: INsufficient permissions");
	} else if (error == SpeechRecognizer.ERROR_NETWORK) {
		Log.d("System.out", "Error: Other network Error");
	} else if (error == SpeechRecognizer.ERROR_NETWORK_TIMEOUT) {
		Log.d("System.out", "Error: Network operation timed out");
	} else if (error == SpeechRecognizer.ERROR_NO_MATCH) {
		Log.d("System.out", "Error: No recognition result matched");
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_RECOGNIZER_BUSY) {
		Log.d("System.out", "Error: Recognition service busy");
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_SERVER) {
		Log.d("System.out", "Error: Server Error");
		failedOfflineLanguage = true;
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_SPEECH_TIMEOUT) {
		Log.d("System.out", "Error: NO speech input");
		isListening = true;
		restartListening();
	}
	}catch(Exception e){Log.e("micError",e.getMessage());}
}
 
Example 14
Source File: MicConfiguration.java    From BotLibre with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void onError(int error) {
	isRecording = false;

	lastReply = System.currentTimeMillis();
	
	this.speech.destroy();
	this.speech = SpeechRecognizer.createSpeechRecognizer(this);
	this.speech.setRecognitionListener(this);

	setMicIcon(false, false);

	muteMicBeep(false);

	setStreamVolume();
	
	if (error == SpeechRecognizer.ERROR_AUDIO) {
		Log.d("System.out", "Error: Audio Recording Error");
	} else if (error == SpeechRecognizer.ERROR_CLIENT) {
		Log.d("System.out", "Error: Other client side error");
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS) {
		Log.d("System.out", "Error: INsufficient permissions");
	} else if (error == SpeechRecognizer.ERROR_NETWORK) {
		Log.d("System.out", "Error: Other network Error");
	} else if (error == SpeechRecognizer.ERROR_NETWORK_TIMEOUT) {
		Log.d("System.out", "Error: Network operation timed out");
	} else if (error == SpeechRecognizer.ERROR_NO_MATCH) {
		Log.d("System.out", "Error: No recognition result matched");
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_RECOGNIZER_BUSY) {
		Log.d("System.out", "Error: Recognition service busy");
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_SERVER) {
		Log.d("System.out", "Error: Server Error");
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_SPEECH_TIMEOUT) {
		Log.d("System.out", "Error: NO speech input");
		restartListening();
	}
	
}
 
Example 15
Source File: ChatActivity.java    From BotLibre with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void onError(int error) {
	debug("onError:" + error);
	Log.d("onError Info", "ChatActivity on error executes here!");
	try{
	this.isRecording = false;

	this.lastReply = System.currentTimeMillis();
	this.speech.destroy();
	this.speech = SpeechRecognizer.createSpeechRecognizer(this);
	this.speech.setRecognitionListener(this);
	
	setMicIcon(false, false);

	muteMicBeep(false);

	setStreamVolume();
	
	if (error == SpeechRecognizer.ERROR_AUDIO) {
		Log.d("System.out", "Error: Audio Recording Error");
	} else if (error == SpeechRecognizer.ERROR_CLIENT) {
		Log.d("System.out", "Error: Other client side error");
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS) {
		Log.d("System.out", "Error: INsufficient permissions");
	} else if (error == SpeechRecognizer.ERROR_NETWORK) {
		Log.d("System.out", "Error: Other network Error");
	} else if (error == SpeechRecognizer.ERROR_NETWORK_TIMEOUT) {
		Log.d("System.out", "Error: Network operation timed out");
	} else if (error == SpeechRecognizer.ERROR_NO_MATCH) {
		Log.d("System.out", "Error: No recognition result matched");
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_RECOGNIZER_BUSY) {
		Log.d("System.out", "Error: Recognition service busy");
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_SERVER) {
		Log.d("System.out", "Error: Server Error");
		failedOfflineLanguage = true;
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_SPEECH_TIMEOUT) {
		Log.d("System.out", "Error: NO speech input");
		isListening = true;
		restartListening();
	}
	}catch(Exception e){Log.e("micError",e.getMessage());}
}
 
Example 16
Source File: MicConfiguration.java    From BotLibre with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void onError(int error) {
	isRecording = false;

	lastReply = System.currentTimeMillis();
	
	this.speech.destroy();
	this.speech = SpeechRecognizer.createSpeechRecognizer(this);
	this.speech.setRecognitionListener(this);

	setMicIcon(false, false);

	muteMicBeep(false);

	setStreamVolume();
	
	if (error == SpeechRecognizer.ERROR_AUDIO) {
		Log.d("System.out", "Error: Audio Recording Error");
	} else if (error == SpeechRecognizer.ERROR_CLIENT) {
		Log.d("System.out", "Error: Other client side error");
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS) {
		Log.d("System.out", "Error: INsufficient permissions");
	} else if (error == SpeechRecognizer.ERROR_NETWORK) {
		Log.d("System.out", "Error: Other network Error");
	} else if (error == SpeechRecognizer.ERROR_NETWORK_TIMEOUT) {
		Log.d("System.out", "Error: Network operation timed out");
	} else if (error == SpeechRecognizer.ERROR_NO_MATCH) {
		Log.d("System.out", "Error: No recognition result matched");
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_RECOGNIZER_BUSY) {
		Log.d("System.out", "Error: Recognition service busy");
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_SERVER) {
		Log.d("System.out", "Error: Server Error");
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_SPEECH_TIMEOUT) {
		Log.d("System.out", "Error: NO speech input");
		restartListening();
	}
	
}
 
Example 17
Source File: ChatActivity.java    From BotLibre with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void onError(int error) {
	debug("onError:" + error);
	Log.d("onError Info", "ChatActivity on error executes here!");
	try{
	this.isRecording = false;

	this.lastReply = System.currentTimeMillis();
	this.speech.destroy();
	this.speech = SpeechRecognizer.createSpeechRecognizer(this);
	this.speech.setRecognitionListener(this);
	
	setMicIcon(false, false);

	muteMicBeep(false);

	setStreamVolume();
	
	if (error == SpeechRecognizer.ERROR_AUDIO) {
		Log.d("System.out", "Error: Audio Recording Error");
	} else if (error == SpeechRecognizer.ERROR_CLIENT) {
		Log.d("System.out", "Error: Other client side error");
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS) {
		Log.d("System.out", "Error: INsufficient permissions");
	} else if (error == SpeechRecognizer.ERROR_NETWORK) {
		Log.d("System.out", "Error: Other network Error");
	} else if (error == SpeechRecognizer.ERROR_NETWORK_TIMEOUT) {
		Log.d("System.out", "Error: Network operation timed out");
	} else if (error == SpeechRecognizer.ERROR_NO_MATCH) {
		Log.d("System.out", "Error: No recognition result matched");
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_RECOGNIZER_BUSY) {
		Log.d("System.out", "Error: Recognition service busy");
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_SERVER) {
		Log.d("System.out", "Error: Server Error");
		failedOfflineLanguage = true;
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_SPEECH_TIMEOUT) {
		Log.d("System.out", "Error: NO speech input");
		isListening = true;
		restartListening();
	}
	}catch(Exception e){Log.e("micError",e.getMessage());}
}
 
Example 18
Source File: SelfAwareConditions.java    From Saiy-PS with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Utility method to construct the {@link SpeechRecognizer} instance
 *
 * @param recognitionListener the {@link SaiyRecognitionListener}
 * @return the {@link Pair} containing the {@link SpeechRecognizer} and Intent with extras
 */
public Pair<SpeechRecognizer, Intent> getNativeRecognition(
        @NonNull final SaiyRecognitionListener recognitionListener) {

    final long then = System.nanoTime();

    SpeechRecognizer recognizer = null;

    final List<ResolveInfo> recognitionServices = mContext.getPackageManager().queryIntentServices(
            new Intent(RecognitionService.SERVICE_INTERFACE), 0);

    if (UtilsList.notNaked(recognitionServices)) {

        String packageName;
        String serviceName;
        ServiceInfo serviceInfo;

        for (final ResolveInfo info : recognitionServices) {

            serviceInfo = info.serviceInfo;
            packageName = serviceInfo.packageName;
            serviceName = serviceInfo.name;

            if (packageName != null && serviceName != null) {
                if (DEBUG) {
                    MyLog.i(CLS_NAME, "getNativeRecognition: Recognizer: " + packageName + " : " + serviceName);
                }

                if (packageName.startsWith(PACKAGE_NAME_GOOGLE)) {
                    recognizer = SpeechRecognizer.createSpeechRecognizer(mContext,
                            new ComponentName(packageName, serviceName));
                    break;
                }
            }
        }

    } else {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "getNativeRecognition: recognitionServices: naked");
        }

        return null;
    }

    if (recognizer == null) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "getNativeRecognition: recognizer: null");
        }

        return null;
    }

    recognizer.setRecognitionListener(recognitionListener);

    if (DEBUG) {
        MyLog.getElapsed(CLS_NAME, "getNativeRecognition", then);
    }

    return new Pair<>(recognizer, getNativeIntent());
}
 
Example 19
Source File: ChatActivity.java    From BotLibre with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void onError(int error) {
	debug("onError:" + error);
	Log.d("onError Info", "ChatActivity on error executes here!");
	try{
	isRecording = false;

	lastReply = System.currentTimeMillis();
	this.speech.destroy();
	this.speech = SpeechRecognizer.createSpeechRecognizer(this);
	this.speech.setRecognitionListener(this);
	
	setMicIcon(false, false);

	muteMicBeep(false);

	setStreamVolume();
	
	if (error == SpeechRecognizer.ERROR_AUDIO) {
		Log.d("System.out", "Error: Audio Recording Error");
	} else if (error == SpeechRecognizer.ERROR_CLIENT) {
		Log.d("System.out", "Error: Other client side error");
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS) {
		Log.d("System.out", "Error: INsufficient permissions");
	} else if (error == SpeechRecognizer.ERROR_NETWORK) {
		Log.d("System.out", "Error: Other network Error");
	} else if (error == SpeechRecognizer.ERROR_NETWORK_TIMEOUT) {
		Log.d("System.out", "Error: Network operation timed out");
	} else if (error == SpeechRecognizer.ERROR_NO_MATCH) {
		Log.d("System.out", "Error: No recognition result matched");
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_RECOGNIZER_BUSY) {
		Log.d("System.out", "Error: Recognition service busy");
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_SERVER) {
		Log.d("System.out", "Error: Server Error");
		failedOfflineLanguage = true;
		restartListening();
	} else if (error == SpeechRecognizer.ERROR_SPEECH_TIMEOUT) {
		Log.d("System.out", "Error: NO speech input");
		isListening = true;
		restartListening();
	}
	}catch(Exception e){Log.e("micError",e.getMessage());}
}
 
Example 20
Source File: ServiceBasedSpeechRecognizer.java    From appinventor-extensions with Apache License 2.0 4 votes vote down vote up
@Override
public void start() {
  speech = SpeechRecognizer.createSpeechRecognizer(container.$context());
  speech.setRecognitionListener(this);
  speech.startListening(recognizerIntent);
}