首页 > 学院 > 开发设计 > 正文

google语音识别启动方式

2019-11-09 16:17:36
字体:
来源:转载
供稿:网友
1.非Intent方式,没有google语音识别的界面
PRivate void initGoogleSpeechRecognizer() {    speechGoogle = android.speech.SpeechRecognizer.createSpeechRecognizer(this);    speechGoogle.setRecognitionListener(recognitionListener);    recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, Locale.getDefault().toString());    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault().toString());    // only for 6.0    recognizerIntent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true);    recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName());    recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);}
    private RecognitionListener recognitionListener = new RecognitionListener() {        @Override        public void onBeginningOfSpeech() {            Log.d("GoogleVoice", "onBeginningOfSpeech");//            onSpeechBegin();        }        @Override        public void onBufferReceived(byte[] buffer) {            Log.d("GoogleVoice", "onBufferReceived: " + Arrays.toString(buffer));        }        @Override        public void onEndOfSpeech() {            Log.d("GoogleVoice", "onEndOfSpeech");
           // onSpeechEnd();        }        @Override        public void onError(int errorCode) {            String errorMessage = getErrorText(errorCode);            Log.e("GoogleVoice", "onError : " + errorMessage);
            // onSpeechError();        }        @Override        public void onEvent(int arg0, Bundle arg1) {            Log.d("GoogleVoice", "onEvent");        }        @Override        public void onPartialResults(Bundle arg0) {            Log.d("GoogleVoice", "onPartialResults");        }        @Override        public void onReadyForSpeech(Bundle arg0) {            Log.d("GoogleVoice", "onReadyForSpeech");          //  onSpeechBegin();                   }        @Override        public void onResults(Bundle results) {            ArrayList<String> matches = results.getStringArrayList(android.speech.SpeechRecognizer.RESULTS_RECOGNITION);            if (matches != null && matches.size() > 0) {                Log.d("GoogleVoice", "Google voice onResults:" + matches.get(0));   
 		// onSpeechResult(matches.get(0));            } else {                Log.e("GoogleVoice", "Google voice onResults:" + " results error");            }        }        @Override        public void onRmsChanged(float rmsdB) {            // Log.i(LOG_TAG, "onRmsChanged: " + rmsdB);            //onSpeechChange();        }    };
2.intent方式,有google语音识别默认的界面
final int SPEECHTOTEXT = 1;
//speechGoogle.startListening(recognizerIntent);Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);// Getting an instance of PackageManagerPackageManager pm = getPackageManager();// Querying Package ManagerList<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);if(activities.size()<=0){    Toast.makeText(getBaseContext(),            "No Activity found to handle the action ACTION_RECOGNIZE_SPEECH",            Toast.LENGTH_SHORT).show();    return;}intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition Demo...");startActivityForResult(intent,SPEECHTOTEXT);
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表