How to use the microphone on Android
I have just started to develop my first Android app, and I am having a hard time figuring out how to start the micropho开发者_如何学Gone and have it listen, which is a main feature of my app.
I've searched the Android docs and I can't find much info on this.
Thanks in advance.
Maybe this can help (actually from the Android docs):
Audio Capture
- Create a new instance of
android.media.MediaRecorder
. - Set the audio source using
MediaRecorder.setAudioSource()
. You will probably want to useMediaRecorder.AudioSource.MIC
. - Set output file format using
MediaRecorder.setOutputFormat()
. - Set output file name using
MediaRecorder.setOutputFile()
. - Set the audio encoder using
MediaRecorder.setAudioEncoder()
. - Call
MediaRecorder.prepare()
on theMediaRecorder
instance. - To start audio capture, call
MediaRecorder.start()
. - To stop audio capture, call
MediaRecorder.stop()
. - When you are done with the
MediaRecorder
instance, callMediaRecorder.release()
on it. CallingMediaRecorder.release()
is always recommended to free the resource immediately.
or:
Android Audio Recording Tutorial
You can use custom recorder:
final static int RQS_RECORDING = 1;
Uri savedUri;
Button buttonRecord;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
buttonRecord = (Button) findViewById(R.id.record);
buttonRecord.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(
MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(intent, RQS_RECORDING);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == RQS_RECORDING) {
savedUri = data.getData();
Toast.makeText(MainActivity.this,
"Saved: " + savedUri.getPath(), Toast.LENGTH_LONG).show();
}
}
精彩评论