Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play video using AVPlayer

I am getting frame buffer one by one from video file using AVAssetReader and doing some operation on the frame and then saving new frame to temp file using AVAssetWritter.Now I have temp file path where all new frame is saving one by one. Is there any way to play video at the time frames is continuously adding to temp file??

here is code to play video from temp path(where frames is continuously adding)

- (void)loadAssetFromFile {

    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:[(mMediaReader.mCameraRecorder) tempVideoFilePath ]] options:nil];
    NSString *tracksKey = @"tracks";

    [asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:tracksKey] completionHandler:
     ^{

         // Completion handler block.
         dispatch_async(dispatch_get_main_queue(),
                        ^{
                            NSError *error = nil;
                            AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey error:&error];

                            if (status == AVKeyValueStatusLoaded) {
                                self.mPlayerItem = [AVPlayerItem playerItemWithAsset:asset];
                                [mPlayerItem addObserver:self forKeyPath:@"status"
                                                options:0 context:&ItemStatusContext];
                                [[NSNotificationCenter defaultCenter] addObserver:self
                                                                         selector:@selector(playerItemDidReachEnd:)
                                                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                                                           object:mPlayerItem];
                               self.mPlayer = [AVPlayer playerWithPlayerItem:mPlayerItem];
                                [mPlayerView setPlayer:mPlayer];
                                [self play:nil];
                            }
                            else {
                                // You should deal with the error appropriately.
                                NSLog(@"The asset's tracks were not loaded:\n%@", [error localizedDescription]);
                            }
                        });
     }];
}

- (IBAction)play:sender {
    [mPlayer play];
}

And code inside the block never runs.

like image 212
iOSPawan Avatar asked Dec 26 '11 04:12

iOSPawan


People also ask

How do I play videos on IOS?

Play a videoAs you browse photos and videos in the Photos app, tap a video to play it on your iPhone. While it plays, you can do any of the following: Tap the player controls below the video to pause, unmute, favorite, share, delete, or see video information; tap the screen to hide the player controls.

How do I display video in SwiftUI?

VideoPlayer in SwiftUI This is a wrapped View versus an actual SwiftUI View. import AVKit import SwiftUI struct ContentView: View { var body: some View { VideoPlayer(player: AVPlayer(url: Bundle. main. url(forResource: "movie", withExtension: "mp4")!)) } }


2 Answers

Dividing the video in multiple sub videos worked for me.

What I did instead of saving full video in one temp path. I divided that video in multiple sub videos and then replaced AVPlayerItem property of AVPlayer accordingly.

So now functionality is working same as video streaming . :)

like image 140
iOSPawan Avatar answered Sep 29 '22 01:09

iOSPawan


You can also convert the CMSampleBuffer that the AVAssetReader returns into a CGImage and then a UIImage and display that in a UIImageView, to render the frames as they are pulled out of the original video file.

There is example code inside the AVFoundation Programming Guide that shows how to do this conversion.

like image 37
Chris McGrath Avatar answered Sep 29 '22 01:09

Chris McGrath