我正在尝试在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:]-
无法使用未打开的设备初始化设备输入。
我在开放方法之后探测了我的“成功”变量,这是肯定的。那么为什么该方法认为设备没有打开?
答案 0 :(得分: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];