如何检测如果一个麦克风present android系统中? [英] How to detect if a microphone is present in android?

查看:100
本文介绍了如何检测如果一个麦克风present android系统中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序捕获用户声音输入的语音识别的部分。

I have a voice recognition part in my application to capture users voice input.

这是我做的

Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
startActivityForResult(voiceIntent, REQUEST_CODE);

这工作正常大部分设备,但现在由于平板电脑越来越流行,其中一些没有麦克风,它抛出一个错误

This works fine on most of the devices but now since the tablets are getting popular and some of them do not have a mic, it throws an error

W / dalvikvm(408):主题ID = 1:螺纹   与未捕获的异常退出   (组= 0x40015560)E / AndroidRuntime(   408):致命异常:主要   E / AndroidRuntime(408):   android.content.ActivityNotFoundException:   无活动来处理意向{   ACT = android.speech.action.RECOGNIZE_SPEECH   (有演员)} E / AndroidRuntime(   408):在   android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)   ......

W/dalvikvm( 408): threadid=1: thread exiting with uncaught exception (group=0x40015560) E/AndroidRuntime( 408): FATAL EXCEPTION: main E/AndroidRuntime( 408): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.speech.action.RECOGNIZE_SPEECH (has extras) } E/AndroidRuntime( 408): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408) .....

所以我想检测,如果麦克风是present之前,我让用户访问语音输入功能。我怎样才能检测一个麦克风在设备上present。

So I want to detect if the microphone is present before I let the user access the voice input feature. How can I detect if a microphone is present on the device.

感谢你。

推荐答案

我添加了另一个答案,但是,这只是一个链接,是一段时间后打破,但这里是正确的答案,其中包括了code

I added another answer but that is just a link that was broken after some time but here is the correct answer which includes the code.

这是在code,你将需要使用来启动语音识别意图。这种检查是否有可用于处理语音识别目的的任何意图。

This is the code that you would need to use to start the voice recognizer intent. This checks if there are any intents available to handle the speech recognition intent.

PackageManager pm = getPackageManager();
List<?> activities = pm.queryIntentActivities(
                      new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() > 0) {
    Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
    startActivityForResult(voiceIntent, REQUEST_CODE);

    Toast toast = Toast.makeText(this, "Loading Voice recognizer...", Toast.LENGTH_SHORT);
    toast.show();
} else { 
    Toast.makeText(this, 
                   "This action is not available on this device.", 
                   Toast.LENGTH_SHORT).show();
}

在,你AY还另做检查,看是否话筒本身是present设备上的顶部。

On the top of that you ay also do another check to see if the microphone itself is present on the device.

if (getPackageManager().hasSystemFeature( "android.hardware.microphone")) {
    //Microphone is present on the device
}

这篇关于如何检测如果一个麦克风present android系统中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆