AVPlayer resizeAspect 仅适用于 iPhone X
AVPlayer resizeAspect works only properly on iPhone X
resizeAspect 因为视频引力仅适用于我,当使用 iPhone X 时。
由于某些原因,黑色纵横条仅添加到顶部而不是底部。这是我不使用 iPhone X 时的样子(图像是白色的)
它应该是这样的:
如您所见,在 iPhone X 上,一切看起来都像预期的那样干净和平衡。
我是这样播放视频的:
avPlayerLayer = AVPlayerLayer(player: avPlayer)
avPlayerLayer.frame = PreviewLayer.bounds
avPlayerLayer.videoGravity = .resizeAspect //Will automatically add black bars
PreviewLayer.layer.insertSublayer(avPlayerLayer, at: 0)
let playerItem = AVPlayerItem(url: video)
avPlayer?.replaceCurrentItem(with: playerItem)
avPlayer?.play()
试试这个步骤
1- 检查与安全连接的底部约束是
2- 试试这个代码
import UIKit
import AVKit
class ViewController: UIViewController {
@IBOutlet weak var playerView: UIView!
private var player: AVPlayer!
override func viewDidLoad() {
super.viewDidLoad()
playVideo(from: "Intro.mp4")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
private func playVideo(from file:String) {
let file = file.components(separatedBy: ".")
guard let path = Bundle.main.path(forResource: file[0], ofType:file[1]) else {
debugPrint( "\(file.joined(separator: ".")) not found")
return
}
player = AVPlayer(url: URL(fileURLWithPath: path))
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player.currentItem, queue: .main) { _ in
self.player?.seek(to: kCMTimeZero)
self.player?.play()
}
let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspect
playerLayer.frame = playerView.layer.bounds
playerView.layer.insertSublayer(playerLayer, at: 0)
player.play()
}
}
iphone X
的结果
结果 iphone 7
Note : if you need video full view on all devices try to link Bottom
constraint
and Top constraint
with superview not with safe area
使您的 videoLayer View 成为以下的子视图:
class VideoContainerView: UIView {
var playerLayer: CALayer?
override func layoutSublayers(of layer: CALayer) {
super.layoutSublayers(of: layer)
playerLayer?.frame = self.bounds
}
}
并添加:
self.VideoShareLayer.playerLayer = avPlayerLayer;
插入图层之前。
resizeAspect 因为视频引力仅适用于我,当使用 iPhone X 时。
由于某些原因,黑色纵横条仅添加到顶部而不是底部。这是我不使用 iPhone X 时的样子(图像是白色的)
它应该是这样的:
如您所见,在 iPhone X 上,一切看起来都像预期的那样干净和平衡。
我是这样播放视频的:
avPlayerLayer = AVPlayerLayer(player: avPlayer)
avPlayerLayer.frame = PreviewLayer.bounds
avPlayerLayer.videoGravity = .resizeAspect //Will automatically add black bars
PreviewLayer.layer.insertSublayer(avPlayerLayer, at: 0)
let playerItem = AVPlayerItem(url: video)
avPlayer?.replaceCurrentItem(with: playerItem)
avPlayer?.play()
试试这个步骤
1- 检查与安全连接的底部约束是
2- 试试这个代码
import UIKit
import AVKit
class ViewController: UIViewController {
@IBOutlet weak var playerView: UIView!
private var player: AVPlayer!
override func viewDidLoad() {
super.viewDidLoad()
playVideo(from: "Intro.mp4")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
private func playVideo(from file:String) {
let file = file.components(separatedBy: ".")
guard let path = Bundle.main.path(forResource: file[0], ofType:file[1]) else {
debugPrint( "\(file.joined(separator: ".")) not found")
return
}
player = AVPlayer(url: URL(fileURLWithPath: path))
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player.currentItem, queue: .main) { _ in
self.player?.seek(to: kCMTimeZero)
self.player?.play()
}
let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspect
playerLayer.frame = playerView.layer.bounds
playerView.layer.insertSublayer(playerLayer, at: 0)
player.play()
}
}
iphone X
的结果结果 iphone 7
Note : if you need video full view on all devices try to link
Bottom constraint
andTop constraint
with superview not with safe area
使您的 videoLayer View 成为以下的子视图:
class VideoContainerView: UIView {
var playerLayer: CALayer?
override func layoutSublayers(of layer: CALayer) {
super.layoutSublayers(of: layer)
playerLayer?.frame = self.bounds
}
}
并添加:
self.VideoShareLayer.playerLayer = avPlayerLayer;
插入图层之前。