2016-07-30 13 views
0

내 iOS 앱에서 AVCaptureSession과 오버레이를 사용하여 비디오를 녹화합니다. 그러나 아무것도 내 사진 앨범에 저장되지 않습니다. 여기서 내가 뭘 잘못하고 있니? NSLog를 실행하면 콘솔에 예상 경로가 표시되지만 기록이 끝나 자마자 뷰가로드되는 즉시 didFinishRecording 코드가 실행됩니다.AVCaptureSession 비디오 저장하지 않음

// 
// 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 

답변

0

시도 비디오에 경로 후이 코드를 추가하고 확인 :

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

을 그리고

- (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]; 
    } 
} 

이 당신을 위해 일하는 희망 당신의 ViewController에서이 방법을 추가 할 수 있습니다.!

+0

나는 그것을 추가했다. 그리고 그것은보기가 카메라 롤에 그것을 저장했다고 말한 순간에 경고창을 띄웠다. 그리고 내가 멈춤을 클릭 한 후에 그것이 나타나야 만했을 때. 그러나 시간이 실제로 동영상을 저장하지 못했습니다. – user717452