AVPlayerLayer Position in UIView Layer

匿名 (未验证) 提交于 2019-12-03 01:04:01

问题:

I am writing an application using Swift to view surveillance cameras via HLS. I have the basic device list working and I am able to segue to the Live view and display the stream however, I need to move the AVPlayerLayer and I am having trouble figuring this out. Here is my current code:

    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() 

I want the AVPlayerLayer to be placed 50 points below the top because I have a header for each view scene.

Thank you!

回答1:

So it just so happens that you cannot place a sub layer in a view in a specific position. Here is the solution:

    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() 

I had to set a specific height of the player and then the position.



回答2:

I don't see where you associate the PlayerLayer with a View and/or add it as a subview/layer to anything? You will need to add it as a sublayer.

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() 

I moved the Y position by 50 as you suggested you wanted to do in your question.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!