9

你好,我想用 mediaRecorder 来录制声音。我要保存的格式是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);

我用 this.mediaRecorder.setAudioEncodingBitRate(16),有些设备没问题

mediaRecorder.setAudioEncodingBitRate(12500),somedevice没问题

但我删除了 mediaRecorder.setAudioEncodingBitRate 一些设备没问题

所以我的问题是如何获得默认的 AudioEncodingBitRate。我需要使用哪个参数?

4

3 回答 3

29

您将 AudioEncodingBitRate 设置得太低。我犯了同样的错误:-)

这似乎有效:

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);
}

想法来自这里

加:阅读文档。setAudioSamplingRate 的文档说如下:

采样率实际上取决于录音的格式以及平台的功能。例如,AAC 音频编码标准支持的采样率为 8 到 96 kHz,AMRNB 支持的采样率为 8kHz,AMRWB 支持的采样率为 16kHz。

于 2013-02-04T18:58:54.300 回答
4

我正在使用波纹管配置并提供惊人的清晰录音输出。

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());

但是,如果您在录制的同时播放音频,则在三星设备中会出现一些问题。

[但仅当您同时播放音频和录制两者时再次使用]

于 2017-10-28T17:07:57.943 回答
1

我发现应该从采样率计算编码比特率。

在https://micropyramid.com/blog/understanding-audio-quality-bit-rate-sample-rate/上有一篇很好的关于这些值如何关联的文章

我使用 8:1 压缩来进行高质量的录音。我更喜欢 48 KHz 采样率,但同样的逻辑可以在本文要求的 8000 Hz 采样率下工作。

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);
于 2019-09-16T13:10:21.930 回答