android语音识别 android.speech 包分析

本文深入探讨了Android平台上的语音识别技术,包括通过RecognizerIntent进行的基本交互、自定义RecognitionService的方法及其实现细节、以及如何使用SpeechRecognizer来启动语音识别过程。此外还提到了一些开源库和非Google风格的识别方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。 http://mengxx.blog.51cto.com/2502718/767085

 

 

 

 

android voice recognition,语音识别作为Service实现。 语音识别API在android.speech中 API大致分成应用端API和service端API

 

RecognizerIntent

顾名思义,在自己的程序中,需要语音识别了,发出这个intent,如果已经安装了google voice search应用,谷歌的activity,service就会跳出来帮你。 http://download.youkuaiyun.com/source/2591401有三个例子。 例子1基本就实现了这样一个应用。 例子1实际上很简单,就靠RecognizerIntent,intent发出后,相应的service会处理intent。 基本上是借鉴了google官方sample:http://developer.android.com/resources/articles/speech-input.html 和 http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.html

(运行例子1的前提是,google voice search应用已经安装,这样确保Service已经安装了) 注意,需要微调程序中的RecognizerIntent的三个参数,可以得到不同的运行效果,简单,参考javadoc即可。 

 

  
  1. Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);  
  2. intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
  3.  
  4. RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 

 

以上是最简单的方式,只需要RecognizerIntent一个类就搞定

 

自己实现RecognitionService 

 

如果要自己做一个识别引擎Service,是可以的。Android提供了RecognitionService抽象类  识别引擎作为Service存在,要实现这个抽象类.  This RecognitionService class should be extended only in case you wish to implement a new speech recognizer.  

Android SDK提供了一个sample VoiceRecognitionService extends RecognitionService,是一个假的引擎,识别结果固定为abc或者123。 这个引擎不读取麦克风,也不含麦克风dialog界面。稍候在SpeechRecognizer处介绍如何使用这个假service. 

 

RecognitionService比较朴素,提供三个方法: 

  
  1. abstract void   onStartListening(Intent recognizerIntent, RecognitionService.Callback listener)  
  2. Notifies the service that it should start listening for speech. 开始识别,intent发过来了 
  3.  
  4. abstract void   onStopListening(RecognitionService.Callback listener) 
  5. Notifies the service that it should stop listening for speech. 结束识别 
  6.  
  7. abstract void   onCancel(RecognitionService.Callback listener)  
  8. Notifies the service that it should cancel the speech recognition. 取消识别 

 RecognitionService是无状态的,不能并行开始识别多个音频流,三个方法必须按照开始-->结束/取消,再开始-->结束/取消,这样顺序调用,否则报错。

 

 

谷歌的Service实现,不开源

AOSP里面没有实现RecognitionService的代码。所以默认的AOSP编出来的android SDK都是不带语言识别Service的,SDK桌面的google search widget没有麦克风,就说明识别Service还没装。

google自己实现了RecognitionService,装了voicesearch 2.1.4之后(一般手机都已经预置),桌面的google search widget也会出现小麦克了点击小麦克出现"speak now"对话框。同时settings voice input output的识别引擎选择框会出现google的选项。谷歌的service叫com.google.android.voicesearch.RecognitionService 这个程序也不开源,无法深度定制谷歌那个"speak now"对话框。

 

P.S. google的识别是基于网络的。 google voice search小应用client不开源,网络端更不开源,网络API尚未开放,但有些人已经研究出来了...  refer to: 

http://stackoverflow.com/questions/2080401/is-there-a-speech-to-text-api-by-google

https://github.com/taf2/speech2text 

https://github.com/taf2/audiosplit 

http://mikepultz.com/2011/03/accessing-google-speech-api-chrome-11 比较牛

vlingo virtual assistant也实现了RecognitionService,不开源

 

 

RecognitionService.Callback 的作用

 

上面三个方法都有一个参数是Callback类形。callback类是个二传手,被service调用,并通过ipc转发结果给RecognitionListener。

The RecognitionService.Callback class receives callbacks from the RecognitionService and forwards them to the RecognitionListener

RecognitionListener是用户自己进程中实现的,RecognitionService是在service进程中的,service没法直接调用listener,就通过callback类转发。

callback帮助自己做service的人处理了ipc,aidl等问题,挺好的。

 

SpeechRecognizer  使用service的

 

This class provides access to the speech recognition service. 给应用程序进程用的,方便对RecognitionService的访问

This class's methods must be invoked only from the main application thread. 只有主应用线程中可以调用...为什么呢...可能因为service会弹出识别的dialog...?

Please note that the application must have RECORD_AUDIO permission to use this class. 程序必须有录音权限

packages/inputmethods/LatinIME/java/src/com/android/inputmethod/voice/VoiceInput.java 可以参考  

例子3  MyStt3Activity也是个参考,但要给识别intent加一行,否则出现error 5

intent.putExtra("calling_package", "");

在例子3中,按下speak按钮之后,程序用SpeechRecognizer.startListening(intent); 开启了麦克风等待用户说话,不会弹出google的“speak now”麦克风的dialog,直接说就行了。

如果此时settings里面设定默认engine为demo的com.example.android.voicerecognitionservice假引擎,则干脆不会读取麦克风,直接返回abc或者123的假结果。

我也试验了vlingo virtual assistant的引擎,也可以用。

多个RecognitionService实现并存,是允许的。不通过settings的默认设置,程序化指定Service,怎么做?todo  intent.setClassName不行

RecognizerResultsIntent 略

 

 

 

 

开源SREC库,是nuance贡献给AOSP的

 

利用 srec 里面的动态插入语音标签功能( Voice Enrollment ) , 添加事先录好的普通话短语,之后就可以对其做语音识别了,

例子2 是调用SREC的,可以参考 (由于SREC的API为@Hide,玩转例子2需要点小技巧) 

 

 

非google的风格的识别

最后,有的厂商自己搞voice framework,不走google android.speech的任何代码,从activity到intent到service到widget全都自己设计,也行。超出本文范围。  

 

 

refer to:

http://blog.youkuaiyun.com/lzf_china/article/details/4510980

http://topic.youkuaiyun.com/u/20100817/15/bcc84f6f-1fec-4e73-9dbf-9ef595597926.html 

 

 

 

About Wouldn't your prefer to let your users speak instead of making them type? This plugin uses OS components for speech recognition and send it to your Unity scripts as String objects. Plugin supports: - Android >= 3.0 (haven’t tested below, it might work though… ), - iOS >= 10.0. That doesn’t mean you can’t target iOS lower than 10 - you simply have to prepare fallback code to cover cases when user doesn’t have access to speech recognition (SpeechRecognizer.EngineExists() for the help!). Keep in mind that both iOS and Android might use Internet connection for speech detection, which means it might fail in case there’s no active connection. Plugin doesn’t work in Editor! You have to run your app on real iOS or Android device. MOBILE SPEECH RECOGNIZER - UNITY PLUGIN ?2 Quick Start Open example scene Go to KKSpeechRecognizer/Example folder inside Unity and open ExampleScene: It shows basic usage of a plugin, which is: 1. Detecting if speech recognition exists on user’s device (keep in mind that it won’t be available on e.g. iOS 9 or old Android phones), 2. If it exists, and user clicks on “Start Recording” button it listens for recognized text and displays it on a screen, 3. On Android, speech recognition automatically detects when user finishes speaking, but on iOS we have to wait for user clicking “Stop Recording” to finish whole process (i.e. get final results). Before running it on Android or iOS device you have to… Setup permissions iOS After generating Xcode project (keep in mind that you have to use Xcode 8 or higher) you have to add two permissions keys to your project: MOBILE SPEECH RECOGNIZER - UNITY PLUGIN ?3 NSMicrophoneUsageDescription explanation from Apple docs: This key lets you describe the reason your app accesses any of the the device’s microphones. When the system prompts the user to allow access, this string is displayed as part of the alert. NSSpeechRecognitionUsageDescription explanation from Apple docs: This key lets you describe the reason y
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值