NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

AVPlayer Button

Forums > iOS

Hello,

At the moment I try to add a Button to an AVPlayer. From the documentation it isn't clear to me how you can do this.

Here is an example what I mean:

The apple developer app has this copy button. But it isn't clear to me how to add one by reading just the documentation. It is clearly an AVPlayer and not a custom controller implementation, because a vanilla player looks nearly the same:

Any idea how to archive this without implementing a controller ui by myself? I just want to add a way to adjust video quality without reinventing the wheel.

I was able to place a button there, but I was not able to resize it:

This is what I did to archive this:

let childViewController = SelfSizingHostingController(rootView: self.child)

childViewController.view.translatesAutoresizingMaskIntoConstraints = false
childViewController.view.contentMode = .scaleAspectFitt

if let test = uiViewController.view.subviews.first?.subviews[1].subviews[4] {
    test.contentMode = .scaleAspectFit

    let routePickerView = AVRoutePickerView() // just try to test if only special views work

    for subview in routePickerView.subviews {
      subview.removeFromSuperview()
    }

    routePickerView.addSubview(childViewController.view)

    if let parentVC = test.parentViewController {
      uiViewController.buttonIsAdded = true
      parentVC.addChild(childViewController)

      self.copyConstraints(sourceView: test.subviews.first!, toView: routePickerView)
      test.insertSubview(routePickerView, at: 0)
      childViewController.didMove(toParent: parentVC)
    }
}

~Nils

   

Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.