android 语音识别_在Android上实现连续语音识别

android 语音识别

Voice recognition has gained a lot of traction over the past few years. When building an app where you feel speech recognition would boost your user experience, you can either:

语音识别在过去几年中获得了很大的关注。 在构建您认为语音识别可以提升用户体验的应用程序时,您可以:

Implementing SpeechRecognizer in your Android application is straightforward. I’ll provide a detailed implementation later in the article.

在Android应用程序中实现SpeechRecognizer很简单。 我将在本文后面提供详细的实现。

However, we want continuous voice recognition. Unfortunately, the API doesn’t provide a mechanism to trigger voice recognition using a keyword. All voice recognition systems are based on this pattern, whether it’s “Ok Google” for Google Assistant, “Hey Siri” for iOS, or “Alexa” for Amazon devices.

但是,我们需要连续的语音识别。 不幸的是,API没有提供使用关键字触发语音识别的机制。 所有语音识别系统均基于此模式,无论对于Google Assistant是“ Ok Google”,对于iOS是“ Hey Siri”,还是对于亚马逊设备是“ Alexa”。

For that, the second option should fit our needs. Sadly, Google Assistant remains a closed API and doesn’t offer many possibilities. It provides App Action, but you won’t achieve continuous voice recognition with it.

为此,第二种选择应符合我们的需求。 令人遗憾的是,Google Assistant仍然是封闭的API,并没有提供很多可能性。 它提供了App Action ,但是您将无法实现连续的语音识别。

I was excited when I first came across VoiceInteractionService. It seemed to do what I wanted with the

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android实现语音识别可以使用Android提供的SpeechRecognizer类。下面是一个基本的示例代码: 1. 添加权限 在AndroidManifest.xml文件添加以下权限: ```xml <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.INTERNET" /> ``` 2. 实现语音识别 在需要实现语音识别的Activity,实例化SpeechRecognizer类,并调用startListening()方法开启语音识别: ```java public class MainActivity extends AppCompatActivity { private SpeechRecognizer speechRecognizer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this); speechRecognizer.setRecognitionListener(new RecognitionListener() { @Override public void onReadyForSpeech(Bundle params) { } @Override public void onBeginningOfSpeech() { } @Override public void onRmsChanged(float rmsdB) { } @Override public void onBufferReceived(byte[] buffer) { } @Override public void onEndOfSpeech() { } @Override public void onError(int error) { } @Override public void onResults(Bundle results) { ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); if (matches != null) { String result = matches.get(0); Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show(); } } @Override public void onPartialResults(Bundle partialResults) { } @Override public void onEvent(int eventType, Bundle params) { } }); Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say something"); speechRecognizer.startListening(intent); } @Override protected void onDestroy() { super.onDestroy(); if (speechRecognizer != null) { speechRecognizer.destroy(); } } } ``` 在上面的代码,我们首先实例化SpeechRecognizer类,并设置RecognitionListener监听器。在onResults()方法,我们可以获取到语音识别的结果,并将其显示在Toast。最后,我们使用Intent启动语音识别,并在startListening()方法传入intent参数。 需要注意的是,在Activity销毁时,应该调用speechRecognizer.destroy()方法,以释放资源。 以上是一个基本的语音识别示例,你可以根据实际需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值