【问题标题】:Resizing the player window调整播放器窗口的大小
【发布时间】:2017-11-02 22:29:53
【问题描述】:

我有一个嵌入 AVPlayer 的 UIview。现在当我玩它时,它似乎并没有覆盖整个 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)        }
}

看起来像这样:

我该如何解决这个问题

【问题讨论】:

  • 尝试使用AVLayerVideoGravityResizeAspectFill 而不是AVLayerVideoGravityResizeAspect
  • 试过了。没有运气
  • 留下一个拒绝投票的理由是有道理的,否则这是一个愚蠢的举动

标签: ios swift uiview avplayer


【解决方案1】:

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

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    playIntro()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 2019-05-15
    • 1970-01-01
    • 2021-08-18
    相关资源
    最近更新 更多