Android原生SpeechRecognizer(语音识别)

本文详细介绍了在Android平台上使用原生SpeechRecognizer进行语音识别的挑战和解决方案,包括权限设置、isRecognitionAvailable方法的使用、错误处理以及针对不同厂商设备的适配策略。文章提到了国内环境下可能遇到的问题,如Google服务在国内的限制,以及如何处理如OPPO设备上的特殊情况。

开篇先吐槽下,在Android 平台开发原生的SpeechRecognizer真是难受的,不像ios,无比轻松,平台统一。 由于Android 平台的碎片化问题比较严重,各个厂商都有自己的实现,尤其是语音助手出来以后,每家的语音服务肯定是不一样的。

目前Android原生的SpeechRecognizer做法应该有两种

  1. 默认调用原生SpeechRecognizer,并稍作修改
  2. 调用第三方,科大讯飞,百度等

这两种做法中

  • 1. 在Google原生系统是可以的,但是在国内的环境是需要修改,修改后能保证各个机型基本可以用,至于识别效果就要看各个机型自己实现的怎么样了
  • 2. 最简单省心省力,如果你的项目可以这么做,那么兄弟恭喜你,你是最幸福的

这里我们不讲第三方的,大家可以自己去集成第三方sdk,主要讨论原生的开发

​ 首先权限不要忘记(记得6.0以后动态请求权限)

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
复制代码

​ 在SpeechRecognizer.class有这样SpeechRecognizer .isRecognitionAvailable一个方法

    /**
     * Checks whether a speech recognition service is available on the system. If this method
     * returns {
    
    @code false}, {
    
    @link SpeechRecognizer#createSpeechRecognizer(Context)} will
     * fail.
     * 
     * @param context with which {
    
    @code SpeechRecognizer} will be created
     * @return {
    
    @code true} if recognition is available, {
    
    @code false} otherwise
     */
    public static boolean isRecognitionAvailable(final Context context) {
        final List<ResolveInfo> list = context.getPackageManager().queryIntentServices(
                new Intent(RecognitionService.SERVICE_INTERFACE), 0);
        return list != null && list.size() != 0;
    }
复制代码

​ 该方法在使用语音识别前建议要调用下,该方法是检查当前系统有没有语音识别服务,我相信绝大多数厂商都有这个服务,但是都有自己特别的实现,但是它至少有,有就可以用。但是,你像oppo的7.0以后机器,这个方法调用后就是false,这时候就是毫无办法了。oppo 7.0以后就是这样调用完后返回false,对于 oppo 这种情况,可以在手机上装一个**讯飞语音+**的app,语音识别就可以了,但是这种方法我估计没人会用,用户体验太差。

​ 如果该方法返回false在我们调用*SpeechRecognizer.startListening();*方法的时候会日志中发现这行log

no selected voice recognition service
复制代码

​ 该日志在SpeechRecognizer.startListening(final Intent recognizerIntent)方法中,大家可以进源码查看这里就不贴了。

​ 检查完如果语音识别可用,接下来有两种做法我们一个个来


  1. 直接创建实例启动服务
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(context);
mSpeechRecognizer.setRecognitionListener(this);
复制代码

​ 创建识别实例,并添加监听,这里没有什么问题,在监听中我们可以拿到我们的想要的回调

/**
 * Used for receiving notifications from the SpeechRecognizer when the
 * recognition related events occur. All the callbacks are executed on the
 * Application main thread.
 * 值的注意的是,所有的回调都在主线程
 */
public interface RecognitionListener {
    
  	// 实例准备就绪 
    void onReadyForSpeech(Bundle params);

    
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、付费专栏及课程。

余额充值