【问题标题】:Android MediaRecorder Sampling Rate and NoiseAndroid MediaRecorder 采样率和噪声
【发布时间】:2015-03-15 07:36:33
【问题描述】:

我在使用 Android 的 MediaRecorder 将声音从麦克风录制到 .m4a 文件(AAC-LC、MPEG-4 容器)时遇到问题。从 API 级别 18 开始,默认采样率从 44.1 或 48 kHz(取决于设备)降至仅 8 Hz。如果我使用 MediaRecorder.setAudioSamplingRate 指定采样率,它会使用指定的采样率,但录音中有很多奇怪的噪音。

在 LogCat 中,不时会出现以下警告:

(1) 标签:音频源 文本:AudioRecord 报告溢出

(2) 标签:AudioFlinger 文字:RecordThread:缓冲区溢出

代码如下:

MediaRecorder recorder = new MediaRecorder();
recorder.setAudioChannels(2);
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.setAudioSamplingRate(48000);   // if not specified, defaults to 8kHz, if specified 44.1 or 48 kHz, lots of noise
recorder.setOutputFile("test.m4a");

try {
    recorder.prepare();
    recorder.start();

} catch (IOException ioe) {
    Log.e(TAG, "IOException", ioe);
} catch (IllegalStateException ise) {
    Log.e(TAG, "IllegalStateException", ise);
} catch (Exception e) {
    Log.e(TAG, "Exception", e);
}

非常感谢任何帮助。

【问题讨论】:

  • 为什么在从麦克风录音时将音频通道数设置为 2?
  • 好点 Squonk,谢谢,但没关系。即使我使用 1 个音频通道,问题仍然存在。还有一个发现,在 LogCat 中,我不时看到以下 2 个警告:(1)AudioSource - AudioRecord 报告溢出(2)AudioFlinger - RecordThread:缓冲区溢出
  • 用差异编码器和速率(22050、16000)进行实验。 android docs“媒体格式”中列出的编解码器
  • @Justin:是的,缓冲区溢出/溢出可能会导致样本被丢弃,从而导致奇怪的结果。有时使用 MP3 并不算太糟糕,但 MP4 音频确实会变得一团糟。
  • @Robert - 谢谢。尝试尝试不同的采样率,有类似的噪音问题。看起来每当我指定除默认值 (8kHz) 以外的采样率时,缓冲区就会溢出,因此会出现噪音。

标签: android audio-recording buffer-overflow mediarecorder sampling


【解决方案1】:

经过长时间的研究和尝试,这是我提出的最佳可行解决方案:

mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mRecorder.setAudioEncodingBitRate(384000);
mRecorder.setAudioSamplingRate(44100);

【讨论】:

  • 如果只有某种方法可以查询设备的真实硬件采样率是多少,这样您就可以明智地选择 44100 与 48000,而无需在 audiobuffersize.appspot.com 查找列表,该列表编译自应用程序play.google.com/store/apps/…
  • 哇,这是一个很大的列表!是的,我希望有人能找到它的代码!或者,有人可以创建一个库,其中包含具有每个采样率的模型设备数组,并根据启动应用程序的手机,它可以从数组中设置它吗?
【解决方案2】:

您可以设置 SamplingRate(如您所做的那样)和 EncodingBitRate,您已省略。

我已经能够使用以下方法获得非常高质量的录音:

    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);
    mRecorder.setAudioSamplingRate(48000);
    mRecorder.setAudioEncodingBitRate(384000);

这将使用每个样本 8 位进行编码,这可能超出了大多数设备上麦克风的可用质量。

【讨论】:

  • 但这会发出非常嘈杂的声音,就像 OVER 高音
  • 我完全没有这种经验,而且我已经在合理范围的设备上使用过这段代码。也许某些设备有问题?您是否尝试过将 (a) 48000 调整为 44100,和/或 (b) 384000 调整为 160000
  • 嗯,可能.. 但请检查我的答案,它的工作 100%!
  • 是的,有可能某些硬件支持 44100 比 48000 更好。这当然是我几年前在 Linux 上经常看到的东西,但我认为现在这一切都还很遥远记忆。您正在使用什么硬件?
  • 这里有一些关于选择采样率的有趣信息:developer.android.com/ndk/guides/audio/sampling-audio.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-20
  • 1970-01-01
  • 2019-11-16
  • 2015-10-17
  • 2012-04-03
  • 1970-01-01
相关资源
最近更新 更多