2016-07-30 16 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); 

をそして、あなたの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]; 
    } 
} 

をこのメソッドを追加し、それはあなたのために働く願っています。!

+0

私はそれを追加しました。そして、ビューがカメラロールに保存したと言った瞬間に警告をポップアップし、停止をクリックした後にポップアップするはずでした。しかし、どちらの時間も実際にビデオを保存していませんでした。 – user717452

関連する問題