在AVPlayerLayer上添加AVPlayerViewController的view
当我们将AVPlayer添加到AVPlayerLayer中时,在播放视图中默认情况下不会显示播放、暂停等按钮。解决方法是在AVPlayerLayer上添加AVPlayerViewController的view,这样就可以在播放视图上显示标准的播放控制按钮。
以下是代码示例:
// 创建一个AVPlayer AVPlayer *player = [AVPlayer playerWithURL:[NSURL URLWithString:@"Your video URL"]];
// 创建AVPlayerLayer AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
// 在AVPlayerLayer上添加AVPlayerViewController的view AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init]; [playerViewController setPlayer:player]; [playerViewController.view setFrame:[playerLayer frame]]; [playerLayer addSublayer:[playerViewController view].layer];
// 将AVPlayerLayer添加到视图中 [self.view.layer addSublayer:playerLayer];
这样,播放视图中就会显示标准的播放控制按钮了。
上一篇:AVPlayer添加按钮