Use the headset button to record sound

I have a problem with the center button of the headset. What I'm trying to do is record the sound of the pressed WHILE headset button. When you release the button, sound recording stops. Here is 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 that when I hold my headset button, the microphone somehow mutes, and this leads to an empty sound file, however, without holding this button (for example, if I move the audio editor code to onCreate), everything works fine (sound recorded).

All I want to know is whether it is possible to record the sound with the button on the headset, or is it a hardware problem, and I can do nothing about it.

+4
source share
2 answers

Sorry, but I think this is not possible for technical reasons.

The Android headset jack has 4 connections: Tip = LeftAudio, Ring = RightAudio, Ring2 = Ground, Sleeve = Mic. (Maybe I have Ring2 and Ground). The HeadsetHook button sends its signal, closing Ring2 and Sleeve. Thus, when you click on it, the microphone will be muted, which explains why you are recording silence. Sucks. Unable to execute PushToTalk.

One option for you is to start recording on onKeyDown and stop recording when you click it again. Even this is not easy, because you will get several onKeyDowns if the button is pressed long enough. So skip onKeyDown and use onKeyUp. This avoids the complexity of the Google Now MediaButton environment. Sort of...

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

Please note that during the final press, you will record silence.

+2
source

Include this code in the if condition

now it should be so.

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

update

you stop it yourself in the onKeyUp(int keyCode, KeyEvent event) Event :)

0
source

All Articles