Question

I have a problem with headset center button. What i'm trying to do is to record sound WHILE headset button is pressed. Once the button is released, audio recording stops. Here's my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    text = (TextView) findViewById(R.id.text);

}

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK && shouldContinue == true) {
        shouldContinue = false;
        text.setText("KEY DOWN");

                    audioRecorder = new MediaRecorder();
                    audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    audioRecorder
                            .setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                    audioRecorder
                            .setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                    audioRecorder.setOutputFile(Environment
                            .getExternalStorageDirectory().getAbsolutePath()
                            + "/test.3gp");

                    try {
                        audioRecorder.prepare();

                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    audioRecorder.start();
                }
    return false;
}


public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK) {
        text.setText("KEY UP");
        audioRecorder.stop();
        audioRecorder.release();
        shouldContinue=true;

    }
    return false;
}

The problem is, when i hold my headset button, mic somehow disables and it results in an empty sound file, however without holding that button (if i move audioRecorder code in onCreate, for example) everything works fine (the sound is recorded).

All i want to know whether it's possible or not to record sound with the center headset button pressed down, or it's hardware issue and i can do nothing about it.

Was it helpful?

Solution

Sorry, but I think it can't be done for hardware reasons.

The Android headset jack has 4 connections: Tip=LeftAudio, Ring=RightAudio, Ring2=Ground, Sleeve=Mic. (I may have Ring2 & Ground reversed). The HeadsetHook button sends its signal by shorting out Ring2 and Sleeve. So, while pressed it will short out the mic, which explains why you are recording silence. Sucks. Can't do PushToTalk.

One option for you is to start recording on onKeyDown and stop recording when it is clicked again. Even this isn't straightforward because you will get multiple onKeyDowns if the button is pressed long enough. So skip onKeyDown and use onKeyUp. It avoids the "Google Now" MediaButton hassle. Something like...

boolean recording = false;

// don't need to override onKeyDown()
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK) {
        if (recording)
            stopAndSaveRecording();
        else
            startRecording();
        recording = ! recording;
    }
    return true;
}

Note that you will be recording silence during the ending click.

OTHER TIPS

Include this code in your if condition

now it should be like this.

  if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK && shouldContinue == true && event.getAction() == KeyEvent.ACTION_DOWN)

update

you are stopping it your self in onKeyUp(int keyCode, KeyEvent event) Event :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top