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.
source share