调整播放器 window
Resizing the player window
我有一个 UIview,我在其中嵌入了一个 AVPlayer。现在当我播放它时,它似乎并没有覆盖整个 UIview。下面是代码:
@IBOutlet weak var introVideo: UIView!
var player: AVPlayer!
func playIntro() {
if let moviePath = Bundle.main.path(forResource: "intro", ofType: "mp4") {
let movieURL = URL(fileURLWithPath: moviePath)
player = AVPlayer(url: movieURL)
let playerLayer = AVPlayerLayer()
playerLayer.player = player
playerLayer.frame = introVideo.bounds
playerLayer.backgroundColor = UIColor.clear.cgColor
player.actionAtItemEnd = .none
playerLayer.videoGravity = AVLayerVideoGravityResizeAspect
introVideo.layer.addSublayer(playerLayer)
player.play()
NotificationCenter.default.addObserver(self,
selector: #selector(playerItemDidReachEnd(notification:)),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: player.currentItem) }
}
看起来像这样:
我该如何解决这个问题
我解决了。我在 viewDidLoad()
中调用了这个函数,但它在每次加载时都以某种方式更新了视图。因此,出于这个原因,应该在 viewDidLayoutSubviews()
中更新边界
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
playIntro()
}
我有一个 UIview,我在其中嵌入了一个 AVPlayer。现在当我播放它时,它似乎并没有覆盖整个 UIview。下面是代码:
@IBOutlet weak var introVideo: UIView!
var player: AVPlayer!
func playIntro() {
if let moviePath = Bundle.main.path(forResource: "intro", ofType: "mp4") {
let movieURL = URL(fileURLWithPath: moviePath)
player = AVPlayer(url: movieURL)
let playerLayer = AVPlayerLayer()
playerLayer.player = player
playerLayer.frame = introVideo.bounds
playerLayer.backgroundColor = UIColor.clear.cgColor
player.actionAtItemEnd = .none
playerLayer.videoGravity = AVLayerVideoGravityResizeAspect
introVideo.layer.addSublayer(playerLayer)
player.play()
NotificationCenter.default.addObserver(self,
selector: #selector(playerItemDidReachEnd(notification:)),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: player.currentItem) }
}
看起来像这样:
我该如何解决这个问题
我解决了。我在 viewDidLoad()
中调用了这个函数,但它在每次加载时都以某种方式更新了视图。因此,出于这个原因,应该在 viewDidLayoutSubviews()
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
playIntro()
}