如果您不想使用
RecognizerIntent进行语音识别,则仍然可以使用
SpeechRecognizer该类来进行语音识别。但是,使用该类比使用意图要难一些。最后一点,我强烈建议让用户知道他何时被记录,否则,当他最终发现时,他可能会非常适应。
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "com.domain.app");SpeechRecognizer recognizer = SpeechRecognizer .createSpeechRecognizer(this.getApplicationContext());RecognitionListener listener = new RecognitionListener() { @Override public void onResults(Bundle results) { ArrayList<String> voiceResults = results .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); if (voiceResults == null) { System.out.println("No voice results"); } else { System.out.println("Printing matches: "); for (String match : voiceResults) { System.out.println(match); } } } @Override public void onReadyForSpeech(Bundle params) { System.out.println("Ready for speech"); } @Override public void onError(int error) { System.err.println("Error listening for speech: " + error); } @Override public void onBeginningOfSpeech() { System.out.println("Speech starting"); } @Override public void onBufferReceived(byte[] buffer) { // TODO Auto-generated method stub } @Override public void onEndOfSpeech() { // TODO Auto-generated method stub } @Override public void onEvent(int eventType, Bundle params) { // TODO Auto-generated method stub } @Override public void onPartialResults(Bundle partialResults) { // TODO Auto-generated method stub } @Override public void onRmsChanged(float rmsdB) { // TODO Auto-generated method stub }};recognizer.setRecognitionListener(listener);recognizer.startListening(intent);
重要说明:从UI线程运行此代码,并确保您具有必需的权限。
<uses-permission android:name="android.permission.RECORD_AUDIO" />
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)