简体   繁体   中英

AVPlayer add Button

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:

我的 AVPlayerView

Any idea how I can 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 roughly how I did it:

// uiViewController -> AVPlayerViewController
    
// self.child -> A SwiftUI view

let childViewController = SelfSizingHostingController(rootView: self.child)
                
childViewController.view.translatesAutoresizingMaskIntoConstraints = false

childViewController.view.contentMode = .scaleAspectFit


if let avMobileAuxiliaryControlsView = uiViewController.view.subviews.first?.subviews[1].subviews[4], let parentVC = avMobileAuxiliaryControlsView.parentViewController {
    
    if !avMobileAuxiliaryControlsView.subviews.map({ String(describing: type(of: $0.self)) }).contains("AVControlOverflowButton") {
        return
    }
    
    uiViewController.buttonIsAdded = true
    
    // Remove all constraints
    avMobileAuxiliaryControlsView.removeConstraints(avMobileAuxiliaryControlsView.constraints)
    
    // Save subviews in dictionary for constraints(withVisualFormat:)
    
    var viewDictionary: [String: UIView] = [:]
    
    for view in avMobileAuxiliaryControlsView.subviews {
        let className = String(describing: type(of: view.self))
        viewDictionary[className] = view;
    }
    
    viewDictionary["QualitySelectorView"] = childViewController.view;
    
    print("dict: \(viewDictionary.keys.joined(separator: ", "))")
    
    // Add to view
    
    parentVC.addChild(childViewController)
    avMobileAuxiliaryControlsView.addSubview(childViewController.view)
    childViewController.didMove(toParent: parentVC)
    
    // Update constraints
    
    avMobileAuxiliaryControlsView.addConstraints( NSLayoutConstraint.constraints(withVisualFormat: "H:|\(viewDictionary.keys.map({ "[\($0)]" }).joined(separator: "-"))", options: [], metrics: nil, views: viewDictionary))
}

What is seen on the pictures above is complete custom playerController. If you want to take hold of current implementation of AVPlayerViewController with some features such as interactive dismissal, double-tap, presentation modes etc.. however loosing some of the playback controls, there is an official way for implementing Custom Playback Controls that was presented in Apple WWDC 2019 AVKit presentation starting at 8min35sec. What you need to do:

  1. modally present AVPlayerViewController
  2. set showsPlaybackControls to false
  3. add controls to contentOverlayView

For more info check the WWDC video.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM