【发布时间】:2019-07-14 11:14:27
【问题描述】:
我已经实现了 AV 视频播放器视图,播放器层不适合更大屏幕的 iPhone,例如 6 - 8 plus 和 iPhone XS Max。
我的播放器视图是金色的,这意味着播放器视图的约束正在适应屏幕边缘,所以问题出在 AVPlayerLayer。
请检查我下面的代码,并根据我的代码库建议我使用工作代码 sn-ps。
func setupVideoBg(){
// Load the video from the app bundle.
let videoURL: NSURL = Bundle.main.url(forResource: "video", withExtension: "mov")! as NSURL
player = AVPlayer(url: videoURL as URL)
player?.actionAtItemEnd = .none
player?.isMuted = true
let playerLayer = AVPlayerLayer(player: player)
// playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
playerLayer.videoGravity = AVLayerVideoGravity.resize
playerLayer.zPosition = -1
// playerLayer.frame = CGRect(x: 0, y: 0, width: self.videoView.frame.size.width, height: self.videoView.frame.size.height)
playerLayer.frame = videoView.layer.bounds
// playerLayer.frame = videoView.bounds
videoView.layer.addSublayer(playerLayer)
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
player?.play()
//loop video
NotificationCenter.default.addObserver(self,
selector: #selector
(self.loopVideo),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: nil)
}
@objc func loopVideo() {
player?.seek(to: CMTime.zero)
player?.play()
}
【问题讨论】:
-
显示你的代码如何设置
AVPlayerLayer -
对不起,我忘了添加
-
@Sh_Khan 代码已添加,请查看
-
你在哪里打电话
setupVideoBg?? -
在 viewDidload() 中
标签: ios swift avplayer ios-autolayout avplayerlayer