简体   繁体   English

使用 AVAudioSession 录制 - 为什么我得到的字节数比预期的多?

[英]Recording with AVAudioSession - Why do I get more bytes than expected?

I'm setting up a recording session as follow:我正在设置录音 session,如下所示:

NSString *audioFilePath = [NSTemporaryDirectory() stringByAppendingString:@"temp.bin"];
_audioFileURL = [NSURL fileURLWithPath:audioFilePath];

NSDictionary *recordSettings = @{
        AVFormatIDKey: @(kAudioFormatLinearPCM),
        AVLinearPCMIsBigEndianKey: @NO,
        AVLinearPCMIsFloatKey: @NO,
        AVEncoderAudioQualityKey: @(AVAudioQualityHigh),
        AVEncoderBitRateKey: @128000,
        AVLinearPCMBitDepthKey: @32,
        AVNumberOfChannelsKey: @2,
        AVSampleRateKey: @44100.0f
    };

According to this, the number of bytes should be:据此,字节数应为:

bytes = 4 * 44100 * 2 * 2 = 705600 bytes

But actually when I get the recorded data:但实际上当我得到记录的数据时:

- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
{
  if (flag) {
  
    NSData *recordedAudioData = [NSData dataWithContentsOfURL:_audioFileURL options:0 error:&error];
    NSUInteger recordedAudioLength = [recordedAudioData length];

The recordedAudioLenght is slightly bigger: 709696 bytes . recordedAudioLenght稍大: 709696 bytes

How can I avoid this?我怎样才能避免这种情况? Is the problem how I setup the recording or how I retrieve the data?问题是我如何设置录音或如何检索数据?

The data from AVAudioRecorder is not just raw bytes.来自 AVAudioRecorder 的数据不仅仅是原始字节。 It's in a container, in this case the Apple Core Audio Format or CAF.它位于容器中,在本例中为Apple 核心音频格式或 CAF。 The extra 4kB are the file header and chunk headers, plus padding ("free" chunk).额外的 4kB 是文件 header 和块头,加上填充(“空闲”块)。

I don't believe there's any way to get AVAudioRecorder to output audio samples without a format.我不相信有任何方法可以让 AVAudioRecorder 获得 output 没有格式的音频样本。 You can find many versions of that question. 您可以找到该问题的许多版本。 You would either need to use a lower-level tool like AVAudioSinkNode, or read the file after writing it and extract the samples.您可能需要使用像 AVAudioSinkNode 这样的低级工具,或者在写入文件后读取文件并提取样本。

(Note that your AVEncoderBitRateKey and AVEncoderAudioQualityKey keys don't apply here, since this is LPCM. But they're not hurting anything; they're just ignored.) (请注意,您的AVEncoderBitRateKeyAVEncoderAudioQualityKey键不适用于此处,因为这是 LPCM。但它们不会造成任何伤害;它们只是被忽略了。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么需要声明一个AVAudioSession实例? - Why to I need to declare an AVAudioSession instance? 为什么我的图像的每行字节数超过其每像素的字节数乘以其宽度? - Why is my image's Bytes per Row more than its Bytes per Pixel times its Width? 向下滚动时,为什么看到的单元格超出numberOfRowInSection的重运行值? - Why do I see more cell than numberOfRowInSection's rerun value when I scroll down? 使用AVAudioSession进行AAC录音的最佳设置是什么? - What is an optimal setting for AAC voice recording with AVAudioSession? 在iOS中录制和播放所需的不同AVAudioSession - Different AVAudioSession needed for Recording and Playing in IOS AVAudioSession-在后台录制音频被VoIP中断 - AVAudioSession - recording audio in the background gets interrupted by VoIP 如何区分录音和播放之间的AVAudioSession中断 - How to distinguish AVAudioSession interruptions between recording and playing 如何使 AVAudioSession 在静音模式和 with.mixWithOthers 下工作 - How do I make AVAudioSession work in silent mode and with .mixWithOthers 如何使用多个滚动条从ui选择器视图中获取数据 - How do i get the data from a ui picker view with more than one scroller in swift iOS AVAudioSession中断通知无法按预期工作 - iOS AVAudioSession interruption notification not working as expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM