iosavfoundationavplayeravcapturemoviefileoutput

AVPlayer won't play MovieFile


I use AVCaptureMovieFileOutput to capture a video file.

- (void)takeVideoAction:(id)sender {
    NSString *outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"output.mp4"];

    NSFileManager *manager = [[NSFileManager alloc] init];
    if ([manager fileExistsAtPath:outputPath]){
        [manager removeItemAtPath:outputPath error:nil];
    }
   [movieFileOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:outputPath] recordingDelegate:self];
}   

And then attempt to play it with AVPlayer, but nothing happens. From what Ive been reading, I think I need to wait for the file to finish loading in the below method instead of trying to play it right away.

 - (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error{

    AVAsset *asset = [AVAsset assetWithURL:outputFileURL];
    self.playerItem = [[AVPlayerItem alloc]initWithAsset:asset];

    self.player = [[AVPlayer alloc] initWithPlayerItem:self.playerItem];
    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    [[[self view] layer] insertSublayer:self.playerLayer atIndex:(int)self.view.layer.sublayers.count];
    [self.playerLayer setFrame:self.view.frame];
    //self.playerLayer.backgroundColor = [UIColor yellowColor].CGColor;
    [self.player play];

}

Solution

  • When I remove the line :

    NSFileManager *manager = [[NSFileManager alloc] init];
    if ([manager fileExistsAtPath:outputPath]){
        [manager removeItemAtPath:outputPath error:nil];
    }
    

    it works.