Cocoa:AVFoundation - 没有活动/启用连接

时间:2012-07-19 02:01:12

标签: cocoa qtkit

我正在尝试构建一个简单的应用程序,从MacBook的内置iSight摄像头捕获视频。我在开发者网站上查看了几个示例项目,并遵循了这里的教程:Apple's AVFoundation Guide

每次我继续打破AVCaptureMovieFileOutput时,我都会收到未捕获的异常 - 没有活动/启用的连接。我是AV框架的新手,所以我不确定它为什么会识别iSight,允许我将它输入到会话中,允许我为会话制作电影输出但是然后告诉我没有连接?它在寻找什么样的联系? (注意:我的viewcontroller中没有QTMovieView,但我认为我只需要播放,而不是录制)。

我知道iSight正在运作,因为我最近刚刚使用它与Skype。

这是我的相关代码:

thisSession = [[AVCaptureSession alloc] init];

//set presets for this session
if ([thisSession canSetSessionPreset:AVCaptureSessionPreset640x480]) {

    thisSession.sessionPreset = AVCaptureSessionPreset640x480;
    NSLog(@"Session Preset: OK");

    //capture a device - captures all the devices, microphone, camera, etc.
    NSArray *devices = [AVCaptureDevice devices];

    //this will hold our decvice
    AVCaptureDevice* iSightCamera;

    for (AVCaptureDevice *device in devices) {

        //we only want to work with the internal camera
        if ([[device localizedName] isEqualToString:@"Built-in iSight"]) { 

            iSightCamera = device;

            //creating an input of the device for the session
            NSError *error = nil;
            AVCaptureDeviceInput* iSightCameraInput =
            [AVCaptureDeviceInput deviceInputWithDevice:iSightCamera error:&error];
            if (!iSightCameraInput) {
                NSLog(@"Error creating device input: %@", error);
            } else { 
                NSLog(@"iSight device input created!");

                //adding the device input to the session
                if ([thisSession canAddInput:iSightCameraInput]) {

                    [thisSession addInput:iSightCameraInput];
                    NSLog(@"iSight input added to session!");

                    //add the output to the session
                    AVCaptureMovieFileOutput *movieOutput = [[AVCaptureMovieFileOutput alloc] init];

                    if ([thisSession canAddOutput:movieOutput]) {

                        [thisSession beginConfiguration];
                        [thisSession addOutput:movieOutput];
                        [thisSession commitConfiguration];

                        NSLog(@"Movie output added to the session!");

                        //start writing the movie
                        NSURL *movieFolder = [NSURL fileURLWithPath:[@"~/Movies" stringByExpandingTildeInPath]];

                        [movieOutput startRecordingToOutputFileURL:movieFolder recordingDelegate:self];
                    }
                    else {
                        NSLog(@"Error: Could not add movie output to the session.");
                    }
                }
                else {
                    NSLog(@"Error: Could not add iSight to session.");
                }

            }

        }

    }

0 个答案:

没有答案