{"id":322,"date":"2011-08-06T15:52:25","date_gmt":"2011-08-06T07:52:25","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=322"},"modified":"2011-08-10T17:07:40","modified_gmt":"2011-08-10T09:07:40","slug":"cocoa-video-capture","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2011\/08\/06\/cocoa-video-capture\/","title":{"rendered":"Cocoa Video Capture"},"content":{"rendered":"<p>The major references for video capture in Cocoa are the <a href=\"http:\/\/developer.apple.com\/library\/mac\/#documentation\/Cocoa\/Conceptual\/QTKitApplicationProgrammingGuide\/Introduction\/Introduction.html\">QTKit Application Programming Guide<\/a>, and the <a href=\"http:\/\/developer.apple.com\/library\/mac\/#documentation\/Cocoa\/Conceptual\/QTKitApplicationTutorial\/Introduction\/Introduction.html\">QTKit Application Tutorial<\/a>. They have the basic examples to display the camera view in the Cocoa window.<\/p>\n<p>The <a href=\"http:\/\/developer.apple.com\/library\/mac\/#documentation\/Cocoa\/Conceptual\/QTKitApplicationTutorial\/BuildingaSimpleQTKitCaptureApplication\/BuildingaSimpleQTKitCaptureApplication.html\">Simple QTKit Recorder Application<\/a> is the starting point. I&#8217;ll further simplify it to remove the two buttons. Here is the layout sketch from the original sample.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"Simple QTKit Recorder Application\" src=\"http:\/\/developer.apple.com\/library\/mac\/documentation\/Cocoa\/Conceptual\/QTKitApplicationTutorial\/Art\/myrecorder_with_buttons.jpg\" alt=\"\" width=\"477\" height=\"362\" \/><\/p>\n<p>Here is the <a href=\"http:\/\/www.magicandlove.com\/software\/CocoaCapture001.zip\">XCode 4.0 project folder<\/a>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-328\" title=\"Cocoa Video Capture\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2011\/08\/CocoaCapture001.jpg\" alt=\"\" width=\"480\" height=\"360\" srcset=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2011\/08\/CocoaCapture001.jpg 480w, http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2011\/08\/CocoaCapture001-300x225.jpg 300w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The header file<\/p>\n<pre lang=\"objc\">\r\n#import <Foundation\/Foundation.h>\r\n#import <QTKit\/QTKit.h>\r\n\r\n@interface MyRecorderController : NSObject {\r\n@private\r\n    IBOutlet QTCaptureView      *mCaptureView;\r\n\r\n    QTCaptureSession            *mCaptureSession;\r\n    QTCaptureDeviceInput        *mCaptureDeviceInput;\r\n}\r\n\r\n@end<\/pre>\n<p>&nbsp;<\/p>\n<p>The objective-c file<\/p>\n<pre lang=\"objc\">#import \"MyRecorderController.h\"\r\n\r\n@implementation MyRecorderController\r\n\r\n- (id)init\r\n{\r\n    self = [super init];\r\n    if (self) {\r\n        \/\/ Initialization code here.\r\n    }\r\n    return self;\r\n}\r\n\r\n- (void)dealloc\r\n{\r\n    [mCaptureSession release];\r\n    [mCaptureDeviceInput release];\r\n    [super dealloc];\r\n}\r\n\r\n- (void)awakeFromNib\r\n{\r\n    mCaptureSession = [[QTCaptureSession alloc] init];\r\n    BOOL success = NO;\r\n    NSError *error;\r\n\r\n    QTCaptureDevice *device = [QTCaptureDevice defaultInputDeviceWithMediaType:QTMediaTypeVideo];\r\n    if (device) {\r\n        success = [device open:&amp;error];\r\n        if (!success) {\r\n            NSLog(@\"Device open error.\");\r\n        }\r\n        mCaptureDeviceInput = [[QTCaptureDeviceInput alloc] initWithDevice:device];\r\n        success = [mCaptureSession addInput:mCaptureDeviceInput error:&amp;error];\r\n        if (!success) {\r\n            NSLog(@\"Add input device error.\");\r\n        }\r\n        [mCaptureView setCaptureSession:mCaptureSession];\r\n        [mCaptureSession startRunning];\r\n    }\r\n}\r\n\r\n- (void)windowWillClose:(NSNotification *)notification\r\n\r\n{\r\n    [mCaptureSession stopRunning];\r\n    [[mCaptureDeviceInput device] close];\r\n}\r\n\r\n@end<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The major references for video capture in Cocoa are the QTKit Application Programming Guide, and the QTKit Application Tutorial. They have the basic examples to display the camera view in the Cocoa window. The Simple QTKit Recorder Application is the starting point. I&#8217;ll further simplify it to remove the two buttons. Here is the layout [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[72,74],"class_list":["post-322","post","type-post","status-publish","format-standard","hentry","category-testing","tag-cocoa","tag-quicktime"],"_links":{"self":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/322","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/comments?post=322"}],"version-history":[{"count":10,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/322\/revisions"}],"predecessor-version":[{"id":324,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/322\/revisions\/324"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=322"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}