2016-07-30 38 views
0

In meiner iOS App verwende ich AVCaptureSession und ein Overlay, um Videos aufzunehmen. In meinem Fotoalbum wird jedoch nichts gespeichert. Was mache ich hier falsch? Wenn ich den NSLog ausführe, zeigt die Konsole den erwarteten Pfad, aber es führt den didFinishRecording-Code aus, sobald die Ansicht geladen wird, und nicht am Ende der Aufzeichnung.AVCaptureSession Video nicht speichern

// 
// LiveCam.m 
// P3 Media 
// 
// Created by Candace Brassfield on 7/29/16. 
// Copyright © 2016 316 Apps. All rights reserved. 
// 

#import "LiveCam.h" 

@interface LiveCam() 

@end 

@implementation LiveCam 
@synthesize session; 
-(void)viewWillAppear:(BOOL)animated { 
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft]; 
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 
    NSDate *today = [NSDate date]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"MMM d hh:mm:ss a"]; 
    // display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings 
    NSString *currentTime = [dateFormatter stringFromDate:today]; 
    NSError* error4 = nil; 
    AVAudioSession* audioSession = [AVAudioSession sharedInstance]; 
    [audioSession setCategory:AVAudioSessionCategoryAmbient error:&error4]; 
    OSStatus propertySetError = 0; 
    UInt32 allowMixing = true; 
    propertySetError |= AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck, sizeof(allowMixing), &allowMixing); 

    // Activate the audio session 
    error4 = nil; 
    if (![audioSession setActive:YES error:&error4]) { 
     NSLog(@"AVAudioSession setActive:YES failed: %@", [error4 localizedDescription]); 
    } 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectoryPath = [paths objectAtIndex:0]; 

    self.session = [[AVCaptureSession alloc] init]; 
    [self.session beginConfiguration]; 
    self.session.sessionPreset = AVCaptureSessionPreset1280x720; 
    self.navigationController.navigationBarHidden = YES; 

    NSError *error = nil; 

    AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio]; 
    NSError *error2 = nil; 
    AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&error2]; 


    AVCaptureDevice *device; 
    AVCaptureDevicePosition desiredPosition = AVCaptureDevicePositionBack; 
    // find the front facing camera 

    device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 


    // get the input device 
    AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; 




    AVCaptureMovieFileOutput *movieFileOutput = [[AVCaptureMovieFileOutput alloc] init]; 

    NSString *archives = [documentsDirectoryPath stringByAppendingPathComponent:@"archives"]; 
    NSString *editedfilename = [[@"ComeOnDown" lastPathComponent] stringByDeletingPathExtension]; 
    NSString *datestring = [[editedfilename stringByAppendingString:@" "] stringByAppendingString:currentTime]; 
    NSLog(@"%@", datestring); 
    NSString *outputpathofmovie = [[archives stringByAppendingPathComponent:datestring] stringByAppendingString:@".mp4"]; 
    NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputpathofmovie]; 
    [self.session addInput:audioInput]; 
    [self.session addInput:deviceInput]; 
    [self.session addOutput:movieFileOutput]; 
    [self.session commitConfiguration]; 
    [self.session startRunning]; 




    AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session]; 
    previewLayer.backgroundColor = [[UIColor blackColor] CGColor]; 
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
    previewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight; 

    CALayer *overlayLayer = [CALayer layer]; 
    CALayer *overlayLayer2 = [CALayer layer]; 

    UIImage *overlayImage = [UIImage imageNamed:@"LiveBorder.png"]; 

    [overlayLayer setContents:(id)[overlayImage CGImage]]; 
    overlayLayer.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height); 
    [overlayLayer setMasksToBounds:YES]; 

     CALayer *rootLayer = [self.view layer]; 
    [rootLayer setMasksToBounds:YES]; 
    [previewLayer setFrame:[rootLayer bounds]]; 
    [rootLayer addSublayer:previewLayer]; 
    [rootLayer addSublayer:overlayLayer]; 
    [rootLayer addSublayer:label2.layer]; 
     movieFileOutput.movieFragmentInterval = kCMTimeInvalid; 

    [movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self]; 



    //self.session = nil; 
    if (error) { 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: 
            [NSString stringWithFormat:@"Failed with error %d", (int)[error code]] 
                  message:[error localizedDescription] 
                  delegate:nil 
                cancelButtonTitle:@"Dismiss" 
                otherButtonTitles:nil]; 
     [alertView show]; 

    } 
    [super viewWillAppear:YES]; 
} 
-(void)viewWillDisappear:(BOOL)animated { 
    self.navigationController.navigationBarHidden = NO; 

} 

-(void)captureOutput:(AVCaptureFileOutput *)captureOutput didStartRecordingToOutputFileAtURL:(NSURL *)fileURL fromConnections:(NSArray *)connections { 

} 
-(void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{ 
    NSLog(@"Finished with error: %@", error); 
} 
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error { 
    //finished 
    NSString *proud = [[NSString alloc] initWithString:[outputFileURL path]]; 
    NSLog(@"Finished%@", proud); 

    UISaveVideoAtPathToSavedPhotosAlbum(proud, self, @selector(video:didFinishSavingWithError: contextInfo:), (__bridge void *)(proud)); 
} 

-(IBAction)goBackNow { 
    [myAVPlayer stop]; 

    [self.session stopRunning]; 
    // [captureView performSelector:@selector(stopRecording) withObject:nil afterDelay:0.0]; 

    NSString *outputPath = [[NSString alloc] initWithFormat:@"%@/%@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0], @"output.mp4"]; 
    UISaveVideoAtPathToSavedPhotosAlbum(outputPath, self, @selector(video:didFinishSavingWithError: contextInfo:), (__bridge void *)(outputPath)); 

    [self.navigationController popToRootViewControllerAnimated:YES]; 
    self.navigationController.navigationBarHidden = NO; 

    //[self.navigationController popToRootViewControllerAnimated:YES]; 
} 
-(void)allDone { 

} 
- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


@end 

Antwort

0

Versuchen Sie diesen Code nach dem Weg zu Video hinzufügen und überprüfen:

UISaveVideoAtPathToSavedPhotosAlbum(url.path, self, @selector(video:didFinishSavingWithError:contextInfo:), nil); 

Und fügen Sie diese Methode in Ihrem Viewcontroller

- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo: (void *) contextInfo { 
    if (error == nil) { 
     [[[UIAlertView alloc] initWithTitle:@"Saved to camera roll" message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; 
    } else { 
     [[[UIAlertView alloc] initWithTitle:@"Failed to save" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; 
    } 
} 

Hoffe, dass es für Sie arbeitet.!

+0

Ich fügte hinzu, und es tauchte die Warnung in dem Moment auf, in dem die Ansicht erschien, sagend, dass es sie in Kamerarolle speicherte, UND als es aufgetaucht sein sollte, nachdem ich geklickt habe, zu stoppen. Allerdings hat keine der beiden Zeiten das Video gespeichert. – user717452