[英]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.) (请注意,您的
AVEncoderBitRateKey
和AVEncoderAudioQualityKey
键不适用于此处,因为这是 LPCM。但它们不会造成任何伤害;它们只是被忽略了。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.