开发者

Android MediaRecorder - SetAudioEncodingBitRate - HowTo [duplicate]

开发者 https://www.devze.com 2023-02-01 19:36 出处:网络
This question already has answers here: Improve Android Audio Recording quality? (3 answers) Closed 3 years ago.
This question already has answers here: Improve Android Audio Recording quality? (3 answers) Closed 3 years ago.

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!

0

精彩评论

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