Guest User

Untitled

a guest
Oct 20th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. #import "AppDelegate.h"
  2.  
  3.  
  4.  
  5. @interface AppDelegate () <AVCaptureFileOutputRecordingDelegate>
  6.  
  7. @end
  8.  
  9. @implementation AppDelegate
  10. {
  11. AVCaptureSession* session;
  12. AVCaptureMovieFileOutput* movieFileOutput;
  13. }
  14.  
  15. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
  16. {
  17. // Insert code here to initialize your application
  18.  
  19. session = [AVCaptureSession new];
  20.  
  21. CGDirectDisplayID disp = [[[NSScreen mainScreen] deviceDescription][@"NSScreenNumber"] unsignedIntValue];
  22.  
  23.  
  24. AVCaptureScreenInput *screenInput = [[AVCaptureScreenInput alloc] initWithDisplayID:disp];
  25.  
  26. if ([session canAddInput:screenInput])
  27. {
  28. [session addInput:screenInput];
  29. }
  30. else
  31. {
  32. NSAlert* alert = [[NSAlert alloc] init];
  33. [alert setMessageText:@"Video recording error"];
  34. [alert setInformativeText:@"Program can't record video"];
  35. return;
  36. }
  37.  
  38. movieFileOutput = [AVCaptureMovieFileOutput new];
  39.  
  40.  
  41. if ([session canAddOutput:movieFileOutput])
  42. [session addOutput:movieFileOutput];
  43.  
  44. NSError* error;
  45. AVCaptureDevice* au1 = [AVCaptureDevice deviceWithUniqueID:@"AppleHDAEngineInput:1B,0,1,0:1"];
  46. AVCaptureDeviceInput* audio1 = [[AVCaptureDeviceInput alloc] initWithDevice:au1 error:&error];
  47.  
  48. if ([session canAddInput:audio1])
  49. {
  50. [session addInput:audio1];
  51. }
  52.  
  53. NSURL *fileURL = [NSURL fileURLWithPath:@"test_movie_fine.mp4"];
  54. [[NSFileManager defaultManager] removeItemAtURL:fileURL error:nil];
  55.  
  56. fileURL = [NSURL fileURLWithPath:@"test_movie_bugged.mp4"];
  57. [[NSFileManager defaultManager] removeItemAtURL:fileURL error:nil];
  58.  
  59. [self performSelector:@selector(recordingStart) withObject:nil afterDelay:1];
  60. [self performSelector:@selector(stopVideo) withObject:nil afterDelay:6];
  61. }
  62.  
  63. int recordedVideos = 0;
  64.  
  65. -(void)stopVideo
  66. {
  67. if (movieFileOutput != nil)
  68. {
  69. recordedVideos++;
  70. [movieFileOutput stopRecording];
  71. }
  72. }
  73.  
  74. -(void)recordingStart
  75. {
  76. [session startRunning];
  77. NSURL *fileURL = [NSURL fileURLWithPath:@"test_movie_fine.mp4"];
  78. [[NSFileManager defaultManager] removeItemAtURL:fileURL error:nil];
  79. [movieFileOutput startRecordingToOutputFileURL:fileURL recordingDelegate:self];
  80. }
  81.  
  82.  
  83.  
  84. -(void)recordingStartFail
  85. {
  86. [session startRunning];
  87. NSURL *fileURL = [NSURL fileURLWithPath:@"test_movie_bugged.mp4"];
  88. [[NSFileManager defaultManager] removeItemAtURL:fileURL error:nil];
  89. [movieFileOutput startRecordingToOutputFileURL:fileURL recordingDelegate:self];
  90.  
  91. [self performSelector:@selector(stopVideo) withObject:nil afterDelay:20];
  92. }
  93.  
  94. - (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error
  95. {
  96. if (error)
  97. {
  98. if (session)
  99. [session stopRunning];
  100. }
  101. [[NSWorkspace sharedWorkspace] openURL:outputFileURL];
  102. if (recordedVideos == 1)
  103. [self recordingStartFail];
  104. }
  105.  
  106.  
  107. - (void)applicationWillTerminate:(NSNotification *)aNotification {
  108. // Insert code here to tear down your application
  109. }
  110.  
  111.  
  112. @end
Add Comment
Please, Sign In to add comment