本文整理匯總了Java中android.speech.SpeechRecognizer類的典型用法代碼示例。如果您正苦於以下問題:Java SpeechRecognizer類的具體用法?Java SpeechRecognizer怎麽用?Java SpeechRecognizer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
SpeechRecognizer類屬於android.speech包,在下文中一共展示了SpeechRecognizer類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: SpeechRecognizerManager
import android.speech.SpeechRecognizer; //導入依賴的package包/類
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();
}
示例2: onCreate
import android.speech.SpeechRecognizer; //導入依賴的package包/類
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHeadPhoneListener = new HeadPhoneListener();
mSpeechStateView = (TextView) findViewById(R.id.stateTextView);
updateUI();
/*
* Init Speech Recognizer
*/
final SpeechRecognizer recognizer = SpeechRecognizer
.createSpeechRecognizer(MainActivity.this);
final Intent recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizerIntent.putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName());
// recognizer.setRecognitionListener(this);
recognizer.setRecognitionListener(new SpeechListener(mHandler));
// onClick(recognizer, recognizerIntent);
permissionsCheck();
registerForIntent();
}
示例3: StudentAdapterViewHolder
import android.speech.SpeechRecognizer; //導入依賴的package包/類
StudentAdapterViewHolder(View itemView) {
super(itemView);
studentImage = (ImageView) itemView.findViewById(R.id.f_data_student_profile);
studentDetail = (TextView) itemView.findViewById(R.id.input_cardview_name);
editText = (EditText) itemView.findViewById(R.id.input_cardview_score);
layout = (LinearLayout) itemView.findViewById(R.id.input_cardview_layout);
txInit = (TextView) itemView.findViewById(R.id.input_cardview_init);
btnMic = (ToggleButton) itemView.findViewById(R.id.input_cardview_mic);
progressBar = (ProgressBar) itemView.findViewById(R.id.progressBarney);
btnMic.setOnCheckedChangeListener(this);
speech = SpeechRecognizer.createSpeechRecognizer(context);
speech.setRecognitionListener(this);
recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, context.getPackageName());
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
}
示例4: onResults
import android.speech.SpeechRecognizer; //導入依賴的package包/類
@Override
public void onResults(Bundle results) {
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
int number = 0;
for (String result : matches) {
try {
number = Integer.parseInt(result);
break;
} catch (NumberFormatException e) {
e.printStackTrace();
number = 0;
}
}
editText.setText(number + "");
isActivated = false;
}
示例5: onCreate
import android.speech.SpeechRecognizer; //導入依賴的package包/類
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_robot);
//hide
ActionBar actionBar = getActionBar();
if (null != actionBar)
actionBar.hide();
mHandler = new responseHandler();
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this, new
ComponentName(
this, VoiceRecognitionService.class));
mSpeechRecognizer.setRecognitionListener(new
CustomRecognitionListener());
initVariables();
initData();
initView();
iniListener();
mSpeechSynthesizer = SpeakerUtils.getInstance(RobotActivity.this).initialTts();
}
示例6: initSpeechRecognizer
import android.speech.SpeechRecognizer; //導入依賴的package包/類
private void initSpeechRecognizer() {
Log.e(TAG, "initSpeechRecognizer: ");
Handler handler = getHandler();
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(mContext);
final Intent recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizerIntent.putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
mContext.getPackageName());
mSpeechRecognizer.setRecognitionListener(new SpeechListener(handler));
mSpeechRecognizer.startListening(recognizerIntent);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mSpeechRecognizer.stopListening();
}
}, DELAY_MILLIS);
}
示例7: createSpeechRecognizer
import android.speech.SpeechRecognizer; //導入依賴的package包/類
@ReactMethod
public void createSpeechRecognizer(final Promise promise) {
if (reactContext == null)
throw new IllegalArgumentException("ReactApplicationContext must be defined!");
if (mSpeechRecognizer != null) {
mSpeechRecognizer.destroy();
mSpeechRecognizer = null;
}
if (SpeechRecognizer.isRecognitionAvailable(reactContext)) {
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(reactContext);
mSpeechRecognizer.setRecognitionListener(new SpeechRecognitionListener(
this.reactContext.getJSModule(RCTNativeAppEventEmitter.class)
));
promise.resolve(null);
} else{
promise.reject("error", "SpeechRecognizer not available");
}
}
示例8: showPlayServicesError
import android.speech.SpeechRecognizer; //導入依賴的package包/類
private void showPlayServicesError(final int errorCode) {
if (DEBUG) {
MyLog.i(CLS_NAME, "showPlayServicesError");
}
onError(SpeechRecognizer.ERROR_CLIENT);
switch (errorCode) {
case UNRECOVERABLE:
// TODO
break;
default:
final GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
apiAvailability.showErrorNotification(mContext, errorCode);
break;
}
}
示例9: onError
import android.speech.SpeechRecognizer; //導入依賴的package包/類
/**
* Receives a terminating error from the stream.
* <p>
* <p>May only be called once and if called it must be the last method called. In particular if an
* exception is thrown by an implementation of {@code onError} no further calls to any method are
* allowed.
* <p>
* <p>{@code t} should be a {@link StatusException} or {@link
* StatusRuntimeException}, but other {@code Throwable} types are possible. Callers should
* generally convert from a {@link Status} via {@link Status#asException()} or
* {@link Status#asRuntimeException()}. Implementations should generally convert to a
* {@code Status} via {@link Status#fromThrowable(Throwable)}.
*
* @param throwable the error occurred on the stream
*/
@Override
public void onError(final Throwable throwable) {
if (DEBUG) {
MyLog.w(CLS_NAME, "onError");
throwable.printStackTrace();
final Status status = Status.fromThrowable(throwable);
MyLog.w(CLS_NAME, "onError: " + status.toString());
}
if (doError.get()) {
doError.set(false);
stopListening();
listener.onError(SpeechRecognizer.ERROR_NETWORK);
}
}
示例10: onError
import android.speech.SpeechRecognizer; //導入依賴的package包/類
/**
* A network or recognition error occurred.
*
* @param error code is defined in {@link SpeechRecognizer}
*/
@Override
public void onError(final int error) {
if (DEBUG) {
MyLog.w(CLS_NAME, "onError: " + error);
MyLog.w(CLS_NAME, "onError: doEndOfSpeech: " + doEndOfSpeech);
MyLog.w(CLS_NAME, "onError: doError: " + doError);
MyLog.i(CLS_NAME, "onError: doBeginningOfSpeech: " + doBeginningOfSpeech);
}
if (error != SpeechRecognizer.ERROR_NO_MATCH) {
doError = true;
}
if (doError) {
onRecognitionError(error);
}
}
示例11: putResults
import android.speech.SpeechRecognizer; //導入依賴的package包/類
/**
* Add the recognition results to the existing {@link Bundle}
*
* @param results recognition results {@link Bundle}
*/
public void putResults(@NonNull final Bundle results) {
if (DEBUG) {
MyLog.i(CLS_NAME, "putResults");
}
final ArrayList<String> heardVoice = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
final float[] confidence = results.getFloatArray(SpeechRecognizer.CONFIDENCE_SCORES);
if (heardVoice != null) {
getBundle().putStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION, heardVoice);
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "putResults: speech empty");
}
getBundle().putStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION, new ArrayList<String>());
}
if (confidence != null) {
getBundle().putFloatArray(SpeechRecognizer.CONFIDENCE_SCORES, confidence);
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "putResults: confidence empty");
}
getBundle().putFloatArray(SpeechRecognizer.CONFIDENCE_SCORES, new float[0]);
}
}
示例12: process
import android.speech.SpeechRecognizer; //導入依賴的package包/類
/**
* Process the extracted text as identified as a command
*
* @param text the command to process
*/
private void process(@NonNull final String text) {
if (DEBUG) {
MyLog.i(CLS_NAME, "process");
}
final Bundle bundle = new Bundle();
final ArrayList<String> voiceResults = new ArrayList<>(1);
voiceResults.add(text);
final float[] confidence = new float[1];
confidence[0] = 1f;
bundle.putStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION, voiceResults);
bundle.putFloatArray(SpeechRecognizer.CONFIDENCE_SCORES, confidence);
bundle.putInt(LocalRequest.EXTRA_CONDITION, Condition.CONDITION_GOOGLE_NOW);
AsyncTask.execute(new Runnable() {
@Override
public void run() {
new RecognitionAction(SaiyAccessibilityService.this.getApplicationContext(), SPH.getVRLocale(SaiyAccessibilityService.this.getApplicationContext()),
SPH.getTTSLocale(SaiyAccessibilityService.this.getApplicationContext()), sl, bundle);
}
});
}
示例13: onError
import android.speech.SpeechRecognizer; //導入依賴的package包/類
@Override
public void onError(int errorCode) {
Log.d(TAG, "in onError");
if ((errorCode == SpeechRecognizer.ERROR_NO_MATCH) || (errorCode == SpeechRecognizer.ERROR_SPEECH_TIMEOUT)) {
stop();
Log.d(TAG, "didn't recognize anything");
// keep going
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
recognizeSpeechDirectly();
} else {
Log.d(TAG, "FAILED " + SpeechRecognitionUtil.diagnoseErrorCode(errorCode));
}
}
示例14: createRecognizer
import android.speech.SpeechRecognizer; //導入依賴的package包/類
public static SpeechRecognizer createRecognizer(AppCompatActivity activity, SpeechActionListener resultHandler, String wakeUpPhrase) {
speechRecognizer =
SpeechRecognizer.createSpeechRecognizer(activity);
loadingRecognizer = new ProgressDialog(activity, R.style.AppCompatAlertDialogStyle);
loadingRecognizer.setIndeterminate(true);
loadingRecognizer.setMessage("Starting up Voice Listener...");
loadingRecognizer.show();
speechRecognizer.setRecognitionListener(new SpeechListener(resultHandler));
wakeUpPhraseText = wakeUpPhrase;
textView = (TextView) activity.findViewById(R.id.spokenText);
intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, activity.getPackageName());
intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "fr-FR");
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "fr-FR");
intent.putExtra(RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE, "fr-FR");
intent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true);
// intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, 10000);
// intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 500);
// intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 2500);
// intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
speechRecognizer.startListening(intent);
return speechRecognizer;
}
示例15: onError
import android.speech.SpeechRecognizer; //導入依賴的package包/類
@Override
public void onError(int error) {
String text = "error" + error;
switch (error) {
case SpeechRecognizer.ERROR_NO_MATCH:
text = "Je n'ai pas compris";
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
if (isAwake) {
text = "Essayez encore s'il vous plaît...";
}
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
}
try {
speechRecognizer.stopListening();
Thread.sleep(2000);
speechRecognizer.startListening(intent);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d(TAG, text);
textView.setText(text);
}