Playback speed control for AVPlayerViewController (Video)

Originator:ricardopereira.eu
Number:rdar://46022646 Date Originated:13-Nov-2018 10:34 AM
Status:Open Resolved:
Product:iOS + SDK Product Version:12.0
Classification:Enhancement Reproducible:Not Applicable
 
Summary:
There’s no easy way to add a Playback Speed button to change the `rate` of the `AVPlayer`. I had to find the "Play/Pause" button checking the Accessible Identifier and add a `UIButton` for changing the speed.

Steps to Reproduce:
So, what I did was basically find that "Play/Pause" button and add my Playback Speed button to the superview of the "Play/Pause" button:

func findPlayPauseButton(view: UIView) -> UIView? {
    if view.accessibilityIdentifier == "Play/Pause" {
        return view
    }
    for subview in view.subviews {
        if let result = findPlayPauseButton(view: subview) {
            return result
        }
    }
    return nil
}

guard let playerView = playerViewController?.view else {
    return
}

guard let playPauseButton = findPlayPauseButton(view: playerView) else {
    return
}

if let playerControlsView = playPauseButton.superview {
    let playbackSpeedButton = UIButton(type: .system)
    playbackSpeedButton.accessibilityIdentifier = "PlaybackSpeed"
    playbackSpeedButton.setTitle("1 X", for: .normal)
    playbackSpeedButton.addTarget(self, action: #selector(self.playbackSpeedButtonTapped), for: .touchUpInside)
    playbackSpeedButton.translatesAutoresizingMaskIntoConstraints = false
    playerControlsView.addSubview(playbackSpeedButton)
    NSLayoutConstraint.activate([
        playbackSpeedButton.heightAnchor.constraint(equalTo: playPauseButton.heightAnchor),
        playbackSpeedButton.widthAnchor.constraint(equalToConstant: 50),
        playbackSpeedButton.leadingAnchor.constraint(equalTo: playPauseButton.trailingAnchor, constant: 70),
        playbackSpeedButton.bottomAnchor.constraint(equalTo: playPauseButton.bottomAnchor),
    ])
}

Expected Results:
I was expecting a simple way to add custom controls or an option to change the playback speed right from the AVPlayerViewController controls.

Actual Results:
As you may know, this has a lot of downsides because of all the edge cases that we will find, i.e., you need to take care the landscape as well, on every new iOS version we need to check if the hack is still valid, etc.

Version:
12.0

Notes:

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!