AVCaptureMovieFileOutput - no active/enabled connections

前端 未结 4 1913
萌比男神i
萌比男神i 2021-02-13 10:43

I am trying to record video in my iPhone app using AVFoundation. But whenever I click the Record button app crashes with this message

-[AVCaptureMovieFile

4条回答
  •  孤独总比滥情好
    2021-02-13 11:40

    Several things are missing in your code :

    1. You forgot to add movieOutput to your captureSession
    2. Same for your audioInput
    3. All your session configuration needs to be encapsulated by [_captureSession beginConfiguration] and [_captureSession commitConfiguration]
    4. For audio recording you need to set the AVAudioSession to the correct category.

    Here is your code updated :

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        NSError *error = nil;
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord
                                               error:&error];
    
        if(!error)
        {
            [[AVAudioSession sharedInstance] setActive:YES error:&error];
    
            if(error) NSLog(@"Error while activating AudioSession : %@", error);
        }
        else
        {
            NSLog(@"Error while setting category of AudioSession : %@", error);
        }
    
        self.captureSession = [[AVCaptureSession alloc] init];
    
        AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
    
        self.videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil];
        self.audioInput = [[AVCaptureDeviceInput alloc] initWithDevice:audioDevice error:nil];
    
        self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
        NSDictionary *stillImageOutputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:
                                                  AVVideoCodecJPEG, AVVideoCodecKey, nil];
        [self.stillImageOutput setOutputSettings:stillImageOutputSettings];
    
        self.movieOutput = [[AVCaptureMovieFileOutput alloc] init];
    
        [self.captureSession beginConfiguration];
    
        [self.captureSession addInput:self.videoInput];
        [self.captureSession addInput:self.audioInput];
        [self.captureSession addOutput:self.movieOutput];
        [self.captureSession addOutput:self.stillImageOutput];
    
        [self.captureSession commitConfiguration];
    
        AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
        previewLayer.frame = CGRectMake(0, 0, 320, 500);
        [self.view.layer addSublayer:previewLayer];
    
        [self.captureSession startRunning];
    }
    
    - (IBAction)toggleRecording:(id)sender
    {
        if(!self.movieOutput.isRecording)
        {
            [self.recordButton setTitle:@"Stop" forState:UIControlStateNormal];
    
            NSString *outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"output.mp4"];
    
            NSFileManager *manager = [[NSFileManager alloc] init];
            if ([manager fileExistsAtPath:outputPath])
            {
                [manager removeItemAtPath:outputPath error:nil];
            }
    
            [self.movieOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:outputPath]
                                          recordingDelegate:self];
        }
        else
        {
            [self.recordButton setTitle:@"Start recording" forState:UIControlStateNormal];
    
            [self.movieOutput stopRecording];
        }
    }
    
    - (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error
    {
        NSLog(@"Did finish recording, error %@ | path %@ | connections %@", error, [outputFileURL absoluteString], connections);
    }
    

    Hope this helps

提交回复
热议问题