Я не могу получить захваченное видео для записи в файл в macOS - PullRequest
0 голосов
/ 04 мая 2020

Я новичок в программировании на MacOS. Я написал код для захвата видео с веб-камеры моей Ma c. Но я все равно не смог записать его в файл. Все, что я получаю, это «Ошибка домена = AVFoundationErrorDomain Code = -11805» от отладчика. Я погуглил и обнаружил, что ошибка 11805 означает, что данные не получены. Кстати, когда я запускаю [session startRunning], свет моей веб-камеры включается. Вот код, который я запускаю. Любая помощь будет оценена. Вот заголовок «Exper2.h»

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>

@interface Recorder : NSObject <AVCaptureFileOutputRecordingDelegate>
{
@private AVCaptureSession* session;
         AVCaptureMovieFileOutput* movieFileOutput;
}
-(void)record:(NSURL *)url;

@end

Вот реализация класса Record

#import "experiment2.h"

@implementation  Recorder


-(void)record:(NSURL* )url{


      session = [[AVCaptureSession alloc] init];
      AVCaptureDevice* videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
      AVCaptureDevice* audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
      NSLog(@"VideoDevice is %@ ", videoDevice);
      NSLog(@"AudioDevice is %@ ", audioDevice);
      AVCaptureDeviceInput* videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil];
      [session addInput:videoInput];
      AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];
      [session addInput:audioInput];

      movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
      [session addOutput:movieFileOutput];
      [session startRunning];
      [movieFileOutput startRecordingToOutputFileURL:url recordingDelegate:self];
       //stop recording after 10 seconds
      [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(finishRecord) userInfo:nil repeats:NO];

}

-(void)finishRecord
  {
    [movieFileOutput stopRecording];
   }

-(void)captureOutput:(AVCaptureFileOutput *)output didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error {

    NSLog(@"Error %@", error);
    NSLog(@"URL %@", outputFileURL);
    [session stopRunning];
}
@end

Выше код скомпилирован как библиотека Dynami c с именем libexperiment2.dylib. Ниже приведен основной код, который импортирует класс Record из моего libexperiment2.dylib

#import <Cocoa/Cocoa.h>
#import <dlfcn.h>
#import "experiment2.h"

int main() {
    NSURL *url = [NSURL fileURLWithPath:@"/Users/nacikurtar/desktop/experiment.mov"];
    void *handle=dlopen("libexperiment2.dylib", RTLD_LAZY);
    Recorder *pointer =[[NSClassFromString(@"Recorder") alloc] init];
    [pointer record:url];
    dlclose(handle);
    return 0;
}

Ниже приведена ошибка, которую я получаю

Error Domain=AVFoundationErrorDomain Code=-11805 "Cannot Record" UserInfo={NSLocalizedRecoverySuggestion=Try recording again., NSLocalizedDescription=Cannot Record, AVErrorRecordingSuccessfullyFinishedKey=false}
2020-05-05 18:32:04.037350+0300 experiment2main[18131:15854628] URL file:///Users/nacikurtar/desktop/experiment.mov
...