8

I am using AVPlayer with AVPlayerViewController. I am playing video after adding watermark in the video. Sometimes it is working fine, but sometimes it shows only a black screen, and audio is fine 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];
Deepak Chaudhary
  • 1,723
  • 1
  • 15
  • 29
  • is this iOS10? have you tried the iOS10.1 beta? – Rhythmic Fistman Oct 07 '16 at 09:50
  • yes, I am working on iOS10, not tried on 10.1 – Deepak Chaudhary Oct 07 '16 at 10:54
  • @DeepakChaudhary have you find the answer? – Pankaj Gupta Nov 21 '16 at 04:59
  • @PankajGupta not exactly, but I did some changes in the same code, which is better then last one: '// show the view controller controller.view.frame =_imgForVidFrame.frame; [self addChildViewController:controller]; [self.view addSubview:controller.view]; [vPlayer play];' – Deepak Chaudhary Nov 25 '16 at 13:26
  • I've faced the same problem and posted about it [here](http://stackoverflow.com/questions/40808377/avplayerlayer-shows-black-screen-but-sound-is-working). Are you facing this issue with local and/or remote videos? – Viktor Gardart Dec 21 '16 at 20:48
  • @ViktorGardart I was working with local video add a sound which is downloaded from URL, then merge with video then play. Now it is working fine, but sometimes, it shows the black screen only on which device has low memory space. – Deepak Chaudhary Dec 24 '16 at 06:57

1 Answers1

0

Make sure that _imgForVidFrame.frame is returning correct frame everytime! You can put breakpoint to that line to check that it is returning proper size or not!

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

Update :

then you can do something like below withou 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 present AVPlayerViewController something like,

 AVPlayer *player = [AVPlayer playerWithURL:_videoFilePath];
 AVPlayerViewController *playerViewController = [AVPlayerViewController new];
 playerViewController.player = player;
 [self presentViewController:playerViewController animated:YES completion:nil];
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75