UIView中的AVPlayer视图缺少控件

时间:2018-10-25 11:48:51

标签: ios objective-c uiview avplayer

在AVPlayer中,如果将AVPlayer添加到UIView,则默认视频前进滑块和播放按钮不显示

NSURL *videourl= [NSURL URLWithString:strUrl];
AVPlayer *player = [AVPlayer playerWithURL:videourl];         

AVPlayerViewController *playerViewController = [AVPlayerViewController new];            

playerViewController.player = player;

playerViewController.view.frame = ViewAV.bounds;

AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = ViewAV.bounds;

playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
//externalPlaybackVideoGravity
playerLayer.contentsGravity = AVLayerVideoGravityResizeAspect;

[ViewAV.layer addSublayer:playerLayer];
[ViewAV addSubview:playerViewController.view];

[playerViewController.player play];

1 个答案:

答案 0 :(得分:0)

尝试将showsPlaybackControlsrequiresLinearPlayback设置为true,

playerViewController.showsPlaybackControls = YES;
playerViewController.requiresLinearPlayback = YES;

当前,您没有将playerViewController添加为子viewController,因此您将看不到任何内容。如下添加

[self addChildViewController:playerViewController];
[self.view.layer addSublayer:playerLayer];
[self.view addSubview:playerViewController.view];