Question

I have a problem, when I try to record and then play the file that was just recorded. I can both record and play the sound but the quality stinks. Its not just bad is really hard to listen to and sound a bit like its a computer generated voice. I use the andriod SDK-emulator. The code that sets up the recording looks like this;

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

And the code playing the file later looks like this;

MediaPlayer mp = new MediaPlayer();
mp.reset();
mp.setDataSource(path);
mp.prepare();
mp.start();

I don't know what part that makes the audio file sound really bad or if its just the emulator that makes it bad and that it would work on a real phone.

Was it helpful?

Solution

You are talking about two different Object, AudioRecorder and MediaRecorder.

OTHER TIPS

Try these

recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);   
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);     
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);   
recorder.setAudioChannels(2);
recorder.setAudioEncodingBitRate(128);
recorder.setAudioSamplingRate(44100);

Incase if you find this code crash on low end android devices then try removing setAudioChannels and setAudioSamplingRate.

It's not just the emulator; it sounds bad on my Nexus 10, too.

Similar to Mohit marwal's answer, you can set the sampling rate and bit rate. 44100 is the standard, CD quality sampling rate. Most commercial audio downloads are encoded at at least 128000 bits per second (not kilobits per second, as shown in Mohit's answer). So those would be good starting values. Also, you should only use one channel, to cut your file size in half, unless you have a stereo mic and need to record in stereo.

recorder.setAudioChannels(1);
recorder.setAudioEncodingBitRate(128000);
recorder.setAudioSamplingRate(44100);

When I set these values, the file size and quality both increased, but still not at the level I would prefer. Perhaps the remaining noise is from a poor microphone on my device, but I haven't tested with an external mic or compared other devices.

AudioRecord takes a variety of arguments that would influence the recording quality.

Try setting these in the constructor:

  • Audio Source: MIC
  • Sample rate: 16000
  • Number of channels: CHANNEL_CONFIGURATION_MONO
  • Format: ENCODING_PCM_16BIT
  • Buffer size: 16000 * 30 (30 second buffer)

Code example:

recorder = new AudioRecord(
                          MediaRecorder.AudioSource.MIC,
                          16000,
                          AudioFormat.CHANNEL_CONFIGURATION_MONO,
                          AudioFormat.ENCODING_PCM_16BIT,
                          16000*30);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top