我想用 AVCaptureSession 录制电影,我也想处理音频部分。(转换为 AAC 并保存)。我从苹果那里读到了这个示例,但我没有设法让它达到我的目的,我认为我不需要音频单元。这是我的代码:
- (void) encodeSample:(CMSampleBufferRef) sample;
{
int nFrame = CMSampleBufferGetNumSamples(sample);
currentSampleTime += (double) nFrame;
AudioTimeStamp ts;
memset(&ts, 0, sizeof(AudioTimeStamp));
ts.mSampleTime = currentSampleTime;
ts.mFlags |= kAudioTimeStampSampleTimeValid;
AudioBufferList lBuffer;
lBuffer.mNumberBuffers = 1;
lBuffer.mBuffers[0].mNumberChannels = 2;
lBuffer.mBuffers[0].mData = malloc(nFrame * sizeof(SInt16) * 2);
lBuffer.mBuffers[0].mDataByteSize = nFrame * sizeof(SInt16) * 2;
size_t bufferListSizeNeededOut;
CMBlockBufferRef blockBufferOut = nil;
OSStatus err = CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sample,
&bufferListSizeNeededOut,
&lBuffer,
sizeof(lBuffer),
kCFAllocatorSystemDefault,
kCFAllocatorSystemDefault,
kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment,
&blockBufferOut);
NSLog(@"lbuff : %ld",err);
@synchronized(self){
if(adtsFile)
{
err = ExtAudioFileWriteAsync(adtsFile, nFrame, &lBuffer);
NSLog(@"async : %ld",err);
}
}
}
但是,我在 3 次调用后得到了 EXC_BAD_ACCESS:
2013-09-04 15:01:35.669 Recoder[13683:1803] lbuff : 0
2013-09-04 15:01:35.673 Recoder[13683:1803] async : 0
2013-09-04 15:01:37.194 Recoder[13683:5b03] lbuff : 0
2013-09-04 15:01:37.197 Recoder[13683:5b03] async : 0
2013-09-04 15:01:37.201 Recoder[13683:5b03] lbuff : 0
// bad access here
我也可以分享我用来初始化我的 ExtAudioFile 的代码,但我相信它运行良好(我在我制作的另一个应用程序中使用它)