Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play video with AVPlayer after AVCaptureMovieFileOutput

I've created a custom camera similar to snapchat with AVFoundation. The picture aspect works: take a picture and I display the picture. Now I'm trying to take a video and display the video.

I have captured a successful video file, tested and works with MPMoviePlayer, but am unable to play it back on AVPlayer.

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

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


            AVURLAsset *asset = [AVURLAsset URLAssetWithURL:outputFileURL options:nil];
            [asset loadValuesAsynchronouslyForKeys:@[@"tracks"] completionHandler:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                 NSError *error = nil;
                 if([asset statusOfValueForKey:@"tracks" error:&error] == AVKeyValueStatusFailed){
                    NSLog(@"error: %@", [error description]);
                }
                else{
                    self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
                    self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
                    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
                    self.playerLayer.frame = self.view.bounds;
                    [self.view.layer addSublayer:self.playerLayer];

                    [self.player play];
                }
            });
        }];
    }

This is my first time using AVFoundation & AVPlayer. Is it that I can not [self.player play] after the asset loads right? I need to wait for item status to be AVPlayerItemStatusReadyToPlay?


ERROR: Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server."

like image 562
Peter Avatar asked Nov 09 '22 03:11

Peter


1 Answers

I am using AVPlayerLayer for playing video. I used the below code and it worked fine.

AVPlayer *avPlayerq = [AVPlayer playerWithURL:NSURL];
avPlayerq.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayerq];
videoLayer.frame= self.view.bounds;
videoLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:videoLayer];
[avPlayerq play];

Hope this may help you.

like image 85
Forte Zhu Avatar answered Nov 15 '22 11:11

Forte Zhu