Vicky
Vicky

Reputation: 1947

Android - How To Detect Whether a Headset Has a Microphone

I want to check whether a headset has a microphone or not. Currently I'm using this broadcast receiver code, but I'm not sure whether it is correct or not.

public class HeadSetMicrophoneStateReceiver extends BroadcastReceiver {

    private String pluggedState = null;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
            int state = intent.getIntExtra("microphone", -1);
            switch (state) {
                case 0:
                    //Headset does not have a Microphone
                    pluggedState = "0";
                    break;
                case 1:
                    //Headset have a Microphone
                    pluggedState = "1";
                    break;
                default:
                    pluggedState = "I have no idea what the headset state is";
            }
            EventBus.getDefault().post(new HeadSetMicrophoneEvent(pluggedState));
        }
    }
}

Please help me out.

Upvotes: 1

Views: 624

Answers (2)

J.D.
J.D.

Reputation: 1411

This method returns whether the microphone is available. If it is not available then an exception will be caught.

public static boolean getMicrophoneAvailable(Context context) {
    MediaRecorder recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    recorder.setOutputFile(new File(context.getCacheDir(), "MediaUtil#micAvailTestFile").getAbsolutePath());
    boolean available = true;
    try { 
        recorder.prepare();
    }
    catch (IOException exception) {
        available = false;
    }
    recorder.release();
    return available;
}

Upvotes: 1

mdDroid
mdDroid

Reputation: 3195

try Following code

ackage com.example.headsetplugtest;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

public class MyActivity extends Activity {

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if (Intent.ACTION_HEADSET_PLUG.equals(action)) {
            Log.d("MyActivity ", "state: " + intent.getIntExtra("state", -1));
            Log.d("MyActivity ", "microphone: " + intent.getIntExtra("microphone", -1));
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
protected void onResume() {
    super.onResume();

    IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    getApplicationContext().registerReceiver(mReceiver, filter);
}

@Override
protected void onStop() {
    super.onStop();

    getApplicationContext().unregisterReceiver(mReceiver);
}
}

Upvotes: 0

Related Questions