Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if a microphone is present in android?

Tags:

android

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

This is what I do

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): 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) .....

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.

Thank you.

like image 341
achie Avatar asked Jan 05 '11 18:01

achie


People also ask

How do I know if my mic is being used?

Open Settings. Scroll down and tap Privacy. Tap either Microphone or Camera. Look through the apps listed and toggle the switch for any apps you don't need the camera or mic to the off position.

Where is the Microphone on Android?

The microphone on Androids is typically at the bottom of your phone. Look at where you plug your phone in, and you'll see some vents or holes. Speak into the mic directly to be heard by others or to speak to your phone.

How do you check which app is using mic in background?

Settings > Apps & Notifications > Scroll down and click Advanced > Permission Manager > Select which settings you'd like to examine, from call logs, to camera permissions, to microphone permissions > Once you're under a category, you can click on any of the apps to toggle the permission to Allow or Deny.


2 Answers

PackageManager pm = getPackageManager();
boolean micPresent = pm.hasSystemFeature(PackageManager.FEATURE_MICROPHONE);

Android API Reference: hasSystemFeature

like image 107
Mark Renouf Avatar answered Sep 19 '22 15:09

Mark Renouf


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.

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();
}

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
}
like image 44
achie Avatar answered Sep 20 '22 15:09

achie