I tried to record audio in Android. The quality of the sound using the MediaRecorder really sucks. Is there a way to improve quality? The documentation says, there should be something like "setAudioEncodingBitRate", but I haven't found a way to implement it.
Then I tried writing the sound to a stream using the AudioRecord fu开发者_开发技巧nction. Great quality but pcm-files are too large in size as I want to upload them to a remote server.
Does anybody know how to either
- improve quality of the AudioRecorder file or
- compress the pcm on the fly (like mp3 or else).
Any help is mostly appreciated.
Yes there are a few things you can change with the audio recorder that will improve the quality. Namely the bitrate/sampling rate and the Audio encoder itself.
Here is a snippet of code that I have been using recently to record audio, with the catches setup for you also.
private void startRecording() {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setAudioEncodingBitRate(16);
recorder.setAudioSamplingRate(44100);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Obviously you will need to call recorder.stop and release it where necessary.
private void stopRecording() {
if (null != recorder) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
}
I still feel there is room for improvement with the quality, so its only a work in progress. Hope that helps!
精彩评论