Article written

  • on 31.12.2010
  • at 06:48 AM
  • by Denis

Android WAV audio recording 0

 In this article I going to solve two issues:
1. Show the way to record WAV file.
2. Record audio with best quality as possible.

 Recently I try to record and then play the file that was just recorded. The main problem in this simple scenario - quality is really bad. It’s not just bad is  really hard to listen to and sound bit like it’s a computer generated  voice... I use the android SDK-emulator. 

So, for recording I using standard class MediaRecorder: 

//Start recording
//...
recorder = new MediaRecorder()
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();

// Stop recording
recorder.stop();
recorder.release();

 I found in the web that there are same limitation:
Nexus One: {44100, 22050, 16000, 11025, 8000}, {mono, stereo}, 16bit all work
Droid: {44100, 22050, 16000, 11025, 8000}, mono, 16bit all work
Emulator: only 8000, mono, 16bit

 First of all we have to forget about MediaRecorder class. Instead we have to use AudioRecord, this class providing more flexibility.
The more detail information about this class you will find on the page: http://developer.android.com/reference/android/media/AudioRecord.html
AudioRecord allow you set the rate of audio quality, choose encoding format and channel configuration.
The example of using this class you will find in bellow.

 The next problem is who to record audio with best quality which will work for all available devices the same way.

I analyzed the Rehearsal Assistant application(http://urbanstew.org/rehearsalassistant/) and use the same principal:
I'm using the loop to create instance of AudioRecord class with decreasing quality until class initialized or rates options finished. Basically it’s looks like:

int[] sampleRates = {44100, 22050, 11025, 8000};
…
int i=0;
do
{ 
     audioRecorder = new AudioRecord(audioSource, sampleRate, channelConfig, audioFormat, bufferSize);
}
while((++i<sampleRates.length) & !( audioRecorder.getState() == AudioRecord.STATE_INITIALIZED))

 

Using this information I created class for audio recording ExtAudioRecorder (source code you will find in attachment).

 Example of using ExtAudioRecorder class: 

// Start recording
extAudioRecorder = ExtAudioRecorder.getInstanse(true);	  // Compressed recording (AMR)
//extAudioRecorder = ExtAudioRecorder.getInstanse(false); // Uncompressed recording (WAV)

extAudioRecorder.setOutputFile(fileName);
extAudioRecorder.prepare();
extAudioRecorder.start();

// Stop recording
extAudioRecorder.stop();
extAudioRecorder.release();

 

Enjoy!

ExtAudioRecorder source

blog comments powered by Disqus

Denis Blog is powered by Drupal
developed by Denis Liger

Valid XHTML 1.0 Strict

Рейтинг@Mail.ru