Avplayer sometimes shows a black screen, but the sound is great

I am using AVPlayer with AVPlayerViewController. I play the video after adding the watermark to the video. Sometimes it works fine, but sometimes it only shows a black screen, and the sound is good in both cases.

vPlayer = [AVPlayer playerWithURL:_videoFilePath]; // create a player view controller AVPlayerViewController *controller = [[AVPlayerViewController alloc]init]; controller.videoGravity=AVLayerVideoGravityResizeAspect; controller.player = vPlayer; [vPlayer seekToTime:kCMTimeZero]; // show the view controller [self addChildViewController:controller]; [vPlayer play]; controller.view.frame =_imgForVidFrame.frame; [self.view addSubview:controller.view]; 
+6
source share
1 answer

Make sure _imgForVidFrame.frame returns the correct frame every time! You can put a breakpoint on this line to make sure that it returns the correct size or not!

and your last line should be [self.view addSubview:controller.view]; not controller.view.frame =_imgForVidFrame.frame; so replace it!

Update:

then you can do something like below using AVPlayerViewController ,

 AVPlayer *player = [AVPlayer playerWithURL:_videoFilePath]; AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player]; playerLayer.frame = self.view.bounds; [self.view.layer addSublayer:playerLayer]; [player play]; 

or you can imagine something like AVPlayerViewController

  AVPlayer *player = [AVPlayer playerWithURL:_videoFilePath]; AVPlayerViewController *playerViewController = [AVPlayerViewController new]; playerViewController.player = player; [self presentViewController:playerViewController animated:YES completion:nil]; 
0
source

All Articles