Android Studio - 语音识别 - onResults
创始人
2024-08-16 04:30:18
0

在Android Studio中,您可以使用SpeechRecognizer类来实现语音识别功能。下面是一个示例代码,演示了如何在onResults方法中处理识别结果:

import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;

// 在您的活动或片段中实例化SpeechRecognizer对象
private SpeechRecognizer speechRecognizer;

// 初始化语音识别
private void initSpeechRecognizer() {
    if (SpeechRecognizer.isRecognitionAvailable(this)) {
        speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
        speechRecognizer.setRecognitionListener(new RecognitionListener() {
            @Override
            public void onResults(Bundle results) {
                // 处理识别结果
                ArrayList matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                if (matches != null && !matches.isEmpty()) {
                    String recognizedText = matches.get(0);
                    // 在这里执行您的操作,例如将识别的文本显示在UI上
                    textView.setText(recognizedText);
                }
            }

            // 其他RecognitionListener方法(onReadyForSpeech,onError,onEnd等)的实现
        });
    }
}

// 开始语音识别
private void startSpeechRecognition() {
    if (speechRecognizer != null) {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
        speechRecognizer.startListening(intent);
    }
}

// 停止语音识别
private void stopSpeechRecognition() {
    if (speechRecognizer != null) {
        speechRecognizer.stopListening();
    }
}

以上代码包含了初始化语音识别和处理识别结果的逻辑。您可以根据自己的需求进行修改和扩展。记得在AndroidManifest.xml文件中添加相应的权限:


希望这可以帮助到您!

相关内容

热门资讯

玩家必备教程!微乐自建房辅助工... 玩家必备教程!微乐自建房辅助工具,微乐小程序免费黑科技(开挂自建房)器1、玩家可以在微乐自建房辅助工...
记者爆料!微信卡五星辅助器,微... 记者爆料!微信卡五星辅助器,微乐自建房脚本免费入口(开挂自建房)软件1、点击下载安装,微信卡五星辅助...
关于!微乐家乡麻将自建房辅助a... 关于!微乐家乡麻将自建房辅助app,微乐家乡麻将自建房辅助app(开挂自建房)技巧1、实时微乐家乡麻...
最新技巧!微乐小程序自建房辅助... 最新技巧!微乐小程序自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)技巧1、很好的工具...
技术分享!微乐小程序麻将自建房... 技术分享!微乐小程序麻将自建房辅助工具插件,微乐小程序免费黑科技(开挂自建房)插件1、每一步都需要思...
专业讨论!微信卡五星辅助器,微... 专业讨论!微信卡五星辅助器,微乐小程序免费黑科技(开挂自建房)工具所有人都在同一条线上,像星星一样排...
一分钟揭秘!微乐自建房辅助工具... 一分钟揭秘!微乐自建房辅助工具脚本,微乐家乡麻将自建房辅助app(开挂自建房)器;小薇(辅助器软件下...
科普常识!微乐小程序自建房辅助... 科普常识!微乐小程序自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)脚本一、微乐小程序...
最新技巧!微乐小程序麻将自建房... 最新技巧!微乐小程序麻将自建房辅助工具插件,微乐小程序免费黑科技(开挂自建房)技巧1、金币登录送、破...
实测发现!微乐自建房辅助工具,... 实测发现!微乐自建房辅助工具,微乐家乡麻将自建房辅助app(开挂自建房)插件;1、打开软件启动之后找...