AVPlayerLayer Position in UIView Layer
Asked Answered
I

2

4

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!

Indignation answered 31/8, 2016 at 21:55 Comment(0)
I
4

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.

Indignation answered 2/9, 2016 at 1:38 Comment(1)
To position layers, you simply use a subclass of UIView and set the position in "layoutSubviews". This is the basic mechanism of laying out views, and why it is named "layoutSubviews".Sheathbill
C
1

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.

Cochlea answered 1/9, 2016 at 0:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.