调整播放器窗口的大小

时间:2017-11-02 22:29:53

标签: ios swift uiview avplayer

我有一个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)        }
}

看起来像这样:

enter image description here

如何解决此问题

1 个答案:

答案 0 :(得分:0)

我解决了。我在viewDidLoad()中调用了这个函数,但它在每次加载时都以某种方式更新了视图。因此,出于这个原因,应该在viewDidLayoutSubviews()

中更新边界
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    playIntro()
}