0

我正在尝试在以下 QTCaptureSession 中添加两个输入:

mainSession = [[QTCaptureSession alloc] init];

BOOL success;
NSError* error;

QTCaptureDevice *videoDevice = [QTCaptureDevice defaultInputDeviceWithMediaType:@"QTMediaTypeVideo"];
success = [videoDevice open:&error];

QTCaptureDevice *audioDevice = [QTCaptureDevice defaultInputDeviceWithMediaType:@"QTMediaTypeSound"];
success = [audioDevice open:&error];

//video = [[QTCaptureDeviceInput alloc] initWithDevice:videoDevice];
//success = [mainSession addInput:video error:&error];

//audio = [[QTCaptureDeviceInput alloc] initWithDevice:audioDevice];
//success = [mainSession addInput:audio error:&error];

output = [[QTCaptureMovieFileOutput alloc] init];
success = [mainSession addOutput:output error:&error];

[output setDelegate:self];

[movieView setCaptureSession:mainSession];

[mainWindow makeKeyAndOrderFront:NSApp];

[mainSession startRunning];

我已经确定注释掉的部分是错误的来源:

[QTCaptureDeviceInput initWithDevice:]-无法使用未打开的设备初始化设备输入。

我在打开方法之后探测了我的“成功”变量,它是肯定的。那么为什么该方法认为设备未打开?

4

1 回答 1

1

如果您还没有找到答案,我认为您的问题实际上在您指出的两行之上。我检查了 Apple 的文档,发现 QTMediaTypeSound 和 QTMediaTypeVideo 是常量,而不是您应该手动传入的字符串。例如,快速的 NSLog() 语句显示 QTMediaTypeVideo 常量实际上等于“vide”。

简而言之,您的代码应该是:

mainSession = [[QTCaptureSession alloc] init];

BOOL success;
NSError* error;

QTCaptureDevice *videoDevice = [QTCaptureDevice defaultInputDeviceWithMediaType:QTMediaTypeVideo];
success = [videoDevice open:&error];

QTCaptureDevice *audioDevice = [QTCaptureDevice defaultInputDeviceWithMediaType:QTMediaTypeSound];
success = [audioDevice open:&error];

video = [[QTCaptureDeviceInput alloc] initWithDevice:videoDevice];
success = [mainSession addInput:video error:&error];

audio = [[QTCaptureDeviceInput alloc] initWithDevice:audioDevice];
success = [mainSession addInput:audio error:&error];

output = [[QTCaptureMovieFileOutput alloc] init];
success = [mainSession addOutput:output error:&error];

[output setDelegate:self];

[movieView setCaptureSession:mainSession];

[mainWindow makeKeyAndOrderFront:NSApp];

[mainSession startRunning];
于 2009-04-27T01:40:15.180 回答