我有一个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) }
}
看起来像这样:
如何解决此问题
答案 0 :(得分:0)
我解决了。我在viewDidLoad()
中调用了这个函数,但它在每次加载时都以某种方式更新了视图。因此,出于这个原因,应该在viewDidLayoutSubviews()
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
playIntro()
}