我製作了一個名爲夜視攝像機的精彩小應用程序,可以記錄夜視效果視頻。目前我正在更新它。用於音頻和視頻組合的AVCaptureSession - 音頻部分提供EXC_BAD_ACCESS
視頻捕捉工作得很好,但音頻沒有。當沒有記錄到文件發生時,只要我打開應用程序就會出現問題(我將更改爲僅在稍後記錄時才激活音頻)。
下面是相關代碼:
session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetMedium;
camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
microphone = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
AVCaptureDeviceInput * camera_input = [AVCaptureDeviceInput deviceInputWithDevice:camera error:nil];
[session addInput:camera_input];
AVCaptureDeviceInput * microphone_input = [AVCaptureDeviceInput deviceInputWithDevice:microphone error:nil];
[session addInput:microphone_input];
AVCaptureVideoDataOutput * output = [[AVCaptureVideoDataOutput alloc] init];
output.videoSettings = [NSDictionary dictionaryWithObject: [NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
[session addOutput:output];
output.minFrameDuration = CMTimeMake(1,30);
dispatch_queue_t queue = dispatch_queue_create("MY QUEUE", NULL);
[output setSampleBufferDelegate:self queue:queue];
dispatch_release(queue);
AVCaptureAudioDataOutput * audio_output = [[AVCaptureAudioDataOutput alloc] init];
[session addOutput:audio_output];
queue = dispatch_queue_create("MY QUEUE", NULL);
AudioOutputBufferDelegate * special_delegate = [[[AudioOutputBufferDelegate alloc] init] autorelease];
special_delegate->normal_delegate = self;
[audio_output setSampleBufferDelegate:special_delegate queue:queue];
dispatch_release(queue);
「特別代表」 是這樣的:
@implementation AudioOutputBufferDelegate
-(void)captureOutput: (AVCaptureOutput *) captureOutput didOutputSampleBuffer: (CMSampleBufferRef) sampleBuffer fromConnection: (AVCaptureConnection *) conenction{
if (normal_delegate->recording) {
[normal_delegate->audio_writer_input appendSampleBuffer: sampleBuffer];
}
}
@end
記錄布爾沒有設置這樣就不追加。您不必擔心AVAssestWriter的設置,因爲它在應用程序崩潰時根本沒有設置。我必須是音頻輸入的設置。
這裏是調用堆棧崩潰時:
> #0 0x33479464 in objc_msgSend
> #1 0x348154b2 in -[AVCaptureAudioDataOutput _AVCaptureAudioDataOutput_AudioDataBecameReady]
> #2 0x34815690 in AVCaptureAudioDataOutput_AudioDataBecameReady
> #3 0x33cc5984 in FigRecorderRemoteCallbacksServer_SampleBuffersArePending
> #4 0x33cc2adc in _XSampleBuffersArePending
> #5 0x33ca42ba in figrecordercallbacks_server
> #6 0x33ca3238 in remrec_ClientPortCallBack
> #7 0x33a5dbe6 in __CFMachPortPerform
> #8 0x33a556fe in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__
> #9 0x33a556c2 in __CFRunLoopDoSource1
> #10 0x33a47f7c in __CFRunLoopRun
> #11 0x33a47c86 in CFRunLoopRunSpecific
> #12 0x33a47b8e in CFRunLoopRunInMode
> #13 0x33b0e4aa in GSEventRunModal
> #14 0x33b0e556 in GSEventRun
> #15 0x32099328 in -[UIApplication _run]
> #16 0x32096e92 in UIApplicationMain
> #17 0x000023e0 in main at main.m:14
感謝您的任何幫助。
你打開殭屍檢測運行你的應用程序嗎?聽起來像是對一個釋放對象的調用。是否[[audio_output setSampleBufferDelegate:special_delegate queue:queue];'保留special_delegate?在此調用之前和之後添加一個斷點並嘗試像'p(int)[special_delegate retaincount]之類的東西。在調試器中。 – emp 2011-03-04 04:36:17
你是對的!謝謝。由於某種原因,它不保留它。我認爲會的。 – 2011-03-04 17:17:52
我面臨同樣的問題。你是如何解決這個問題的?我閱讀了提及您需要保留代表的意見。但我的應用程序正在使用ARC。我該如何解決它?謝謝 – Nitin 2014-03-19 10:06:03