Avplayer有时显示黑屏,但声音很好

时间:2016-10-07 08:31:06

标签: ios avplayer avplayerviewcontroller

我正在使用AVPlayer和AVPlayerViewController。我在视频中添加水印后播放视频。有时它工作正常,但有时它只显示黑屏,两种情况下音频都很好。

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

1 个答案:

答案 0 :(得分:0)

确保_imgForVidFrame.frame每次都返回正确的帧!您可以将断点放到该行以检查它是否返回正确的大小!

,你的最后一行应该是[self.view addSubview:controller.view];而不是controller.view.frame =_imgForVidFrame.frame;所以交换它!

更新:

然后您可以使用AVPlayerViewController

执行以下操作
AVPlayer *player = [AVPlayer playerWithURL:_videoFilePath];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:playerLayer];
[player play];

或者您可以提供AVPlayerViewController之类的内容,

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