Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "AppDelegate.h"
- @interface AppDelegate () <AVCaptureFileOutputRecordingDelegate>
- @end
- @implementation AppDelegate
- {
- AVCaptureSession* session;
- AVCaptureMovieFileOutput* movieFileOutput;
- }
- - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
- {
- // Insert code here to initialize your application
- session = [AVCaptureSession new];
- CGDirectDisplayID disp = [[[NSScreen mainScreen] deviceDescription][@"NSScreenNumber"] unsignedIntValue];
- AVCaptureScreenInput *screenInput = [[AVCaptureScreenInput alloc] initWithDisplayID:disp];
- if ([session canAddInput:screenInput])
- {
- [session addInput:screenInput];
- }
- else
- {
- NSAlert* alert = [[NSAlert alloc] init];
- [alert setMessageText:@"Video recording error"];
- [alert setInformativeText:@"Program can't record video"];
- return;
- }
- movieFileOutput = [AVCaptureMovieFileOutput new];
- if ([session canAddOutput:movieFileOutput])
- [session addOutput:movieFileOutput];
- NSError* error;
- AVCaptureDevice* au1 = [AVCaptureDevice deviceWithUniqueID:@"AppleHDAEngineInput:1B,0,1,0:1"];
- AVCaptureDeviceInput* audio1 = [[AVCaptureDeviceInput alloc] initWithDevice:au1 error:&error];
- if ([session canAddInput:audio1])
- {
- [session addInput:audio1];
- }
- NSURL *fileURL = [NSURL fileURLWithPath:@"test_movie_fine.mp4"];
- [[NSFileManager defaultManager] removeItemAtURL:fileURL error:nil];
- fileURL = [NSURL fileURLWithPath:@"test_movie_bugged.mp4"];
- [[NSFileManager defaultManager] removeItemAtURL:fileURL error:nil];
- [self performSelector:@selector(recordingStart) withObject:nil afterDelay:1];
- [self performSelector:@selector(stopVideo) withObject:nil afterDelay:6];
- }
- int recordedVideos = 0;
- -(void)stopVideo
- {
- if (movieFileOutput != nil)
- {
- recordedVideos++;
- [movieFileOutput stopRecording];
- }
- }
- -(void)recordingStart
- {
- [session startRunning];
- NSURL *fileURL = [NSURL fileURLWithPath:@"test_movie_fine.mp4"];
- [[NSFileManager defaultManager] removeItemAtURL:fileURL error:nil];
- [movieFileOutput startRecordingToOutputFileURL:fileURL recordingDelegate:self];
- }
- -(void)recordingStartFail
- {
- [session startRunning];
- NSURL *fileURL = [NSURL fileURLWithPath:@"test_movie_bugged.mp4"];
- [[NSFileManager defaultManager] removeItemAtURL:fileURL error:nil];
- [movieFileOutput startRecordingToOutputFileURL:fileURL recordingDelegate:self];
- [self performSelector:@selector(stopVideo) withObject:nil afterDelay:20];
- }
- - (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error
- {
- if (error)
- {
- if (session)
- [session stopRunning];
- }
- [[NSWorkspace sharedWorkspace] openURL:outputFileURL];
- if (recordedVideos == 1)
- [self recordingStartFail];
- }
- - (void)applicationWillTerminate:(NSNotification *)aNotification {
- // Insert code here to tear down your application
- }
- @end
Add Comment
Please, Sign In to add comment