我正在尝试构建一个简单的应用程序,从 MacBook 的内置 iSight 摄像头中捕获视频。我查看了开发人员网站上的几个示例项目,并在此处遵循教程:Apple's AVFoundation Guide。
每次我不断中断 AVCaptureMovieFileOutput 时,我都会收到一个未捕获的异常 - 没有活动/启用的连接。我是 AV 框架的新手,所以我不确定它为什么能识别 iSight,允许我将其输入到会话中,允许我为会话制作电影输出,但然后告诉我没有连接?它在寻找什么联系?(注意:我的视图控制器中还没有 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.");
}
}
}
}