dimanche 1 novembre 2015

Stop recording video after 2 seconds in Objective-C

I am trying to make this toggleMovieRecording method stop the recording after 2 seconds. I don't know what I'm doing wrong.

I have created the NSTimer with interval of 2 seconds and the selector is set to run the entire method, which I believe would run through the If statement and toggle the recording off. I think it might be getting caught in a loop but I'm not sure why.

- (IBAction)toggleMovieRecording:(id)sender {

    self.cameraButton.enabled = NO;
    self.recordButton.enabled = NO;
    self.playerButton.enabled = NO;

    dispatch_async( self.sessionQueue, ^{
        if ( ! self.movieFileOutput.isRecording ) {
            [self.locationManager startUpdatingLocation];
            if ( [UIDevice currentDevice].isMultitaskingSupported ) {
                // Setup background task. This is needed because the -[captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error:]
                // callback is not received until AVMetadataRecordPlay returns to the foreground unless you request background execution time.
                // This also ensures that there will be time to write the file to the photo library when AVMetadataRecordPlay is backgrounded.
                // To conclude this background execution, -endBackgroundTask is called in
                // -[captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error:] after the recorded file has been saved.
                self.backgroundRecordingID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
            }

            // Update the orientation on the movie file output video connection before starting recording.
            AVCaptureConnection *connection = [self.movieFileOutput connectionWithMediaType:AVMediaTypeVideo];
            AVCaptureVideoPreviewLayer *previewLayer = (AVCaptureVideoPreviewLayer *)self.previewView.layer;
            connection.videoOrientation = previewLayer.connection.videoOrientation;

            // Turn OFF flash for video recording.
            [AAPLCameraViewController setFlashMode:AVCaptureFlashModeOff forDevice:self.videoDeviceInput.device];

            // Start recording to a temporary file.
            NSString *outputFileName = [NSProcessInfo processInfo].globallyUniqueString;
            NSString *outputFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[outputFileName stringByAppendingPathExtension:@"mov"]];
            [self.movieFileOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:outputFilePath] recordingDelegate:self];
            } else {
            {
                _startTimer = CFBridgingRetain([NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(toggleMovieRecording:) userInfo:nil repeats:NO]);
            }

            (void) _startTimer; {
            [self.movieFileOutput stopRecording];
            [self.locationManager stopUpdatingLocation];
            }
        }
    } );
}

Aucun commentaire:

Enregistrer un commentaire