如何更改AVPlayer视频的内容模式?

时间:2019-05-26 22:33:03

标签: swift

我的问题是,当我显示视频时,它大于屏幕尺寸。也许问题不在于内容模式,而是错误的帧边界或其他内容。

5

我也尝试了let theURL = Bundle.main.url(forResource:"videkas", withExtension: "mp4") avPlayer = AVPlayer(url: theURL!) avPlayerLayer = AVPlayerLayer(player: avPlayer) avPlayerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill avPlayer.volume = 0 avPlayer.actionAtItemEnd = .none avPlayerLayer.frame = (featureView13?.bounds)! featureView13?.backgroundColor = .clear featureView13?.layer.insertSublayer(avPlayerLayer, at: 0) NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidReachEnd(notification:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: avPlayer.currentItem) ,但是没有运气。

4 个答案:

答案 0 :(得分:0)

这可能对您有用 https://gist.github.com/jkosoy/c835fea2c03e76720c77

当视频开始缓冲时调用此方法

DispatchQueue.main.async {
    let size = CGSize.aspectFit(aspectRatio: self. avPlayerLayer.frame.size, boundingSize: self.view.frame.size)
    self.avPlayerLayerHeightContraint.constant = size.height
    self.avPlayerLayer.layoutIfNeeded()
}

希望你得到 理想的结果。

答案 1 :(得分:0)

resizeAspectFill更改为resizeAspect。有很大的不同!

答案 2 :(得分:0)

尝试使用,

avPlayerLayer.frame = (featureView13?.bounds)!
avPlayerLayer.videoGravity = .resizeAspect
avPlayerLayer.contentsGravity = .resizeAspect

答案 3 :(得分:-1)

使用AVLayerVideoGravity.resizeAspectFit在屏幕上放置视频:

avPlayerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFit