gpt4 book ai didi

android - 了解语音识别服务

转载 作者:太空宇宙 更新时间:2023-11-03 10:52:11 25 4
gpt4 key购买 nike

我正在尝试了解 RecognitionService 的功能和 RecognitionService.Callback .我对这个框架很陌生,想知道如何在 RecognitionService 中调用 onStartListening() 函数。我看到帖子How to register a custom speech recognition service?但是我在所有主要函数中都插入了日志消息,以查看何时调用了哪个函数。

我也看过 sdk 中的示例应用程序,但它在解释事情如何发生方面做得很糟糕。我想从一个 Activity 中调用 startService。

我使用以下 Intent

Intent startServiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
startServiceIntent.setClass(this, SimpleVoiceService.class);

startService(startServiceIntent);

有人可以帮我解决这个问题吗?如果有人可以向我指出这方面的教程,或者描述如何操作的一般流程,那就太好了。

非常感谢。

最佳答案

基本思想是使用 SpeechRecognizer 连接到用户在常规 Android 设置中选择的 RecognitionService

SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(context);
sr.setRecognitionListener(new RecognitionListener() {
@Override
public void onResults(Bundle b) { /* ... */ }

// other required methods
});

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "");
sr.startListening(intent);

您必须提供 RecognitionListener 方法的实现,允许您更新 UI 以响应语音识别事件(用户开始说话,部分结果可用,用户停止说话,转录仍在进行中)继续,发生错误等)。

在一些键盘应用程序的源代码中查看完整的实现,例如VoiceInput class in Hacker's Keyboard .

关于android - 了解语音识别服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11652988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com