我的耳机中心按钮有问题。我想做的是在按下耳机按钮时录制声音。松开按钮后,录音停止。这是我的代码:
@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;
}
问题是,当我按住耳机按钮时,麦克风会以某种方式禁用并导致声音文件为空,但是如果不按住该按钮(例如,如果我在 onCreate 中移动 audioRecorder 代码)一切正常(声音被录制)。
我只想知道是否可以在按下中间耳机按钮的情况下录制声音,或者这是硬件问题,我对此无能为力。