开发者

How to record audio file in Android

I am workin开发者_StackOverflow社区g in android. How can I record an audio file through microphone, and how can I save the recorded file in the emulator?


It is easy to record audio in Android. What you need to do is:

1) Create the object for media record class : MediaRecorder recorder = new MediaRecorder();

2) In the emulator, you're unable to store the recorded data in memory, so you have to store it on the SD Card. So, first check for the SD Card availability: then start recording with the following code.

String status = Environment.getExternalStorageState();
if(status.equals("mounted")){
   String path = your path;
}

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();

3) To stop, override stop method of activity

 recorder.stop();
 recorder.release();


Here is a good tutorial with sample code.

Audio Capture at Android Developer

it includes these steps:

  • Create a new instance of android.media.MediaRecorder.
  • Set the audio source using MediaRecorder.setAudioSource(). You will probably want to use MediaRecorder.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 the MediaRecorder instance.
  • To start audio capture, call MediaRecorder.start().
  • To stop audio capture, call MediaRecorder.stop().
  • When you are done with the MediaRecorder instance, call MediaRecorder.release() on it. Calling MediaRecorder.release() is always recommended to free the resource immediately.


Basically, there is no easy way to write audio file as simple WAV file. AudioRecorder produces data in raw Linear PCM format. And Android's API only gives you audio data buffers.

You'll need to create WAV file yourself. What this means for you, is that you need to add all the chunks yourself: RIFF header, FMT and DATA chunks.


I had tried to record the WAV file in android. But somehow, it is only recording the. WAV file in stereo only and you should use the audio recorder with the following parameter

recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
                        RECORDER_SAMPLERATE, AudioFormat.CHANNEL_IN_MONO,RECORDER_AUDIO_ENCODING, bufferSize);

        recorder.startRecording();

and from the recorder, you need to write all the data into one file and also you need to give header information

then just do

recorder.stopRecording();

but I have just one problem in this is even if I give AudioFormat.CHANNEL_IN_MONO..it still record in stereo format. Hope my answer helps you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜