添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

hello,i want to use mediaRecorder to record voice. i want to save the format is amr.

this.mediaRecorder = new MediaRecorder();
this.mediaRecorder.setAudioChannels(1);
this.mediaRecorder.setAudioSamplingRate(8000);
this.mediaRecorder.setAudioEncodingBitRate(16);
this.mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
this.mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
this.mediaRecorder.setOutputFile(this.file.getAbsolutePath());
this.mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

i used this.mediaRecorder.setAudioEncodingBitRate(16), some device is ok

mediaRecorder.setAudioEncodingBitRate(12500),somedevice is ok

but i delete the mediaRecorder.setAudioEncodingBitRate some device is ok

so my question how to get the default the AudioEncodingBitRate. which parameter i need to use?

MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
if (Build.VERSION.SDK_INT >= 10) {
    recorder.setAudioSamplingRate(44100);
    recorder.setAudioEncodingBitRate(96000);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
} else {
    // older version of Android, use crappy sounding voice codec
    recorder.setAudioSamplingRate(8000);
    recorder.setAudioEncodingBitRate(12200);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(file.getAbsolutePath());
try {
    recorder.prepare();
} catch (IOException e) {
    throw new RuntimeException(e);

The idea comes from here

plus: read the docs. The docs of setAudioSamplingRate say the following:

The sampling rate really depends on the format for the audio recording, as well as the capabilities of the platform. For instance, the sampling rate supported by AAC audio coding standard ranges from 8 to 96 kHz, the sampling rate supported by AMRNB is 8kHz, and the sampling rate supported by AMRWB is 16kHz.

well, then just remove the if branch and ust the code from the else part. AMR_NB = Narrowband, it means you cannot use a higher samplingRate than 8k which results in a poorer recording quality. When using the AMR encoder you should use the THREE_GPP format. – Thomas Solti Feb 5, 2013 at 11:46 I still think your main problem was the setAudioEncodingBitRate method. you need to specify the bitrate here. Bitrate = bits/second. 12 bits / sec is not a value that you can use to encode an audio signal. here you find common bitrates for Audio. E.g. 800 bits/sec are minimal neccessary to recognize speech. – Thomas Solti Feb 5, 2013 at 12:03

I am using bellow configurations and gives amazing clear recording output.

localFileName = getFileName()+".wav";
localFile = new File(localdir, localFileName);
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mRecorder.setOutputFormat(AudioFormat.ENCODING_PCM_16BIT);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mRecorder.setAudioChannels(1);
mRecorder.setAudioEncodingBitRate(128000);
mRecorder.setAudioSamplingRate(44100);
mRecorder.setOutputFile(localFile.getPath());

however if you are recording along with playing audio simultaneously it has some issues in samsung devices.

[but again only when you are playing audio and recording both together at the same time]

I find that the encoding bitrate should be calculated from the sample rate.

There is a good write-up of how these values relate on https://micropyramid.com/blog/understanding-audio-quality-bit-rate-sample-rate/

I use 8:1 compression for high-quality recordings. I prefer 48 KHz sampling, but the same logic works at an 8000 Hz sample rate requested for this post.

final int BITS_PER_SAMPLE = 16;       // 16-bit data
final int NUMBER_CHANNELS = 1;        // Mono
final int COMPRESSION_AMOUNT = 8;     // Compress the audio at 8:1
public MediaRecorder setupRecorder(String filename, int selectedAudioSource, int sampleRate) {
    final int uncompressedBitRate = sampleRate * BITS_PER_SAMPLE * NUMBER_CHANNELS;
    final int encodedBitRate = uncompressedBitRate  / COMPRESSION_AMOUNT;
    mediaRecorder = new MediaRecorder();
    try  {
        mediaRecorder.setAudioSource(selectedAudioSource);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        mediaRecorder.setAudioSamplingRate(sampleRate);
        mediaRecorder.setAudioEncodingBitRate(encodedBitRate);
        mediaRecorder.setOutputFile(filename);
    }catch (Exception e) {
        // TODO
    return mediaRecorder;
MediaRecorder mediaRecorder = setupRecorder(this.file.getAbsolutePath(), 
                                   MediaRecorder.AudioSource.MIC, 
                                   8000);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.