这是我尝试在 UI 视图上播放视频的功能,
func playVideo(videoString: String) { `` if let videoURL = URL(string: videoString) { `` self.player = AVPlayer(url: videoURL) `` self.player?.isMuted = true
self.player?.actionAtItemEnd = .none
let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
playerLayer.bounds = CGRect(origin: .zero, size: self.videoView.bounds.size)
playerLayer.position = CGPoint(x: self.videoView.bounds.midX, y: self.videoView.bounds.midY)
self.videoView.layer.addSublayer(playerLayer)
self.player?.play()
NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidReachEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem)
}
}
@objc func playerItemDidReachEnd(notification: NSNotification) {
self.player?.seek(to: CMTime.zero)
self.player?.play()
}
AVPlayerLayer 没有覆盖整个 UI View,如何使其覆盖整个 View?
我试图在 UIView 上播放视频,但 AVPlayerLayer 没有覆盖整个视图。
您需要评论这一行
playerLayer.position = CGPoint(x: self.videoView.bounds.midX, y:self.videoView.bounds.midY)
因为它使
playerLayer
的左上角位于videoView
的中间