在ios中定义AV Player ViewController的视图帧大小

时间:2016-02-26 11:03:54

标签: ios avplayer avplayerviewcontroller

如何在iOS中播放视频作为背景?我遵循了这个问题How to play a local video with Swift?,但AV Player ViewController是一个控制器,我不会让我决定包含视频的视图的位置和大小。它总是需要一些预定义的值。

1 个答案:

答案 0 :(得分:4)

我已经完成了一个我已经完成但没有使用AVPlayerViewController的项目 - 只是我添加到视图层的普通AVPlayer。我有一个集合视图,但您可以在任意位置插入视频。尝试这样的事情:

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"my_local_file" ofType:@"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
self.avPlayerItem = [AVPlayerItem playerItemWithURL:fileURL];
self.avPlayer = [AVPlayer playerWithPlayerItem:self.avPlayerItem];
self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.avPlayerLayer.frame = self.view.bounds;
[self.view.layer insertSublayer:self.avPlayerLayer below:self.collectionView.layer];
// could also try [self.view.layer insertSublayer:self.avPlayerLayer atIndex:0];
[self.avPlayer play];

基本上,将AVPlayerLayer.frame设置为您想要的大小。