如何在 iOS 中将视频作为背景播放?我关注了这个问题 How to play a local video with Swift? ,但 AV Player ViewController 是一个 Controller ,不允许我决定包含视频的 View 的位置和大小。它总是采用一些预定义的值。
最佳答案
我已经为我完成的一个项目工作,但没有使用 AVPlayerViewController - 只是我添加到 View 层的普通 AVPlayer。我有一个 Collection View ,但您可以将视频插入图层中任何您想要的位置。尝试这样的事情:
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 设置为您想要的大小。
关于ios - 在 ios 中为 AV Player ViewController 定义 View 框架大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35650064/