我想用AVCaptureSession录制电影,我也想处理音频部分。 (转换为AAC并保存)。我读过this sample from apple,但我没有设法使它适用于我的目的,我认为我不需要音频单元。这是我的代码:
- (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的代码,但我相信它运行良好(我在另一个应用程序中使用它)