AVPlayerLayer 在 UIView 层的位置

AVPlayerLayer Position in UIView Layer

我正在编写一个应用程序,使用 Swift 通过 HLS 查看监控摄像头。我有基本的设备列表,我可以转至实时视图并显示流,但是,我需要移动 AVPlayerLayer,但我无法解决这个问题。这是我当前的代码:

    let player = AVPlayer(URL: url!)
    let playerLayer = AVPlayerLayer(player: player)
    let view = UIView(frame: CGRectMake(0, 0, screenSize.width, screenSize.height))

    self.view.layer.borderWidth = 1
    self.view.layer.borderColor = UIColor(red:222/255.0, green:225/255.0, blue:227/255.0, alpha: 1.0).CGColor
    self.view.layer.addSublayer(playerLayer)

    playerLayer.frame = view.bounds
    player.play()

我希望将 AVPlayerLayer 放置在顶部下方 50 点,因为每个视图场景都有一个 header。

谢谢!

我没看到你在哪里将 PlayerLayer 与视图相关联 and/or 将其作为 subview/layer 添加到任何内容?您需要将其添加为子图层。

let player = AVPlayer(URL: url!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = CGRectMake(0,50,screenSize.width, screenSize.height)
self.view.layer.addSublayer(playerLayer)
player.play()

我按照你在问题中的建议将 Y 位置移动了 50。

因此,您无法在特定位置的视图中放置子图层。这是解决方案:

    let player = AVPlayer(URL: url!)
    let playerController = AVPlayerViewController()
    playerController.player = player

    playerController.view.frame = CGRectMake(0, 50, screenSize.width, 240)
    self.view.addSubview(playerController.view)
    self.addChildViewController(playerController)

    player.play()

我必须先设置播放器的特定高度,然后再设置位置。