2011-03-04 83 views
3

我制作了一个名为夜视摄像机的精彩小应用程序,可以记录夜视效果视频。目前我正在更新它。用于音频和视频组合的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 

感谢您的任何帮助。

+1

你打开僵尸检测运行你的应用程序吗?听起来像是对一个释放对象的调用。是否[[audio_output setSampleBufferDelegate:special_delegate queue:queue];'保留special_delegate?在此调用之前和之后添加一个断点并尝试像'p(int)[special_delegate retaincount]之类的东西。在调试器中。 – emp 2011-03-04 04:36:17

+0

你是对的!谢谢。由于某种原因,它不保留它。我认为会的。 – 2011-03-04 17:17:52

+0

我面临同样的问题。你是如何解决这个问题的?我阅读了提及您需要保留代表的意见。但我的应用程序正在使用ARC。我该如何解决它?谢谢 – Nitin 2014-03-19 10:06:03

回答

2

这听起来像你的程序访问某种坏记忆。你有没有尝试在你的设置中启用NSZombieEnabled属性?上周我发现了一个EXC_BAD_ACCESS问题。当你这样做时,它应该在导致崩溃的调用中断开。

1

它看起来像你试图访问未准备好或尚未创建的东西。

+0

好的,谢谢。任何想法我错过了什么? – 2011-03-04 01:56:06

1

special_delegate对象需要保留。