可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.