简体   繁体   English

AVPlayerViewController 播放速度的自定义控件

[英]Custom control for playback speed for AVPlayerViewController

  1. Can we add custom playback speed (0.5, 1.0, 1.5, 2.0) control to AVPlayerViewController ?我们可以向AVPlayerViewController添加自定义播放速度 (0.5, 1.0, 1.5, 2.0) 控件吗?
  2. Is there a way we can refresh speed rate using an UISlider value change?有没有办法使用UISlider值更改来刷新速度?
  3. Can we change the playback speed if it is playing currently?如果当前正在播放,我们可以更改播放速度吗?
  1. Can we add custom playback speed (0.5, 1.0, 1.5, 2.0) control to AVPlayerViewController ?我们可以向AVPlayerViewController添加自定义播放速度 (0.5, 1.0, 1.5, 2.0) 控件吗?

You can add a custom playback speed button to AVPlayerViewController by doing a little hack.您可以通过一些小技巧向AVPlayerViewController添加自定义播放速度按钮。

In iOS 11, ie, you can add a button to the AVPlayerViewController by adding the control next to the " Play/Pause " button.在 iOS 11 中,即,您可以通过在“播放/暂停”按钮旁边添加控件来向AVPlayerViewController添加一个按钮。 That button has an Accessible Identifier which will help you find that control.该按钮有一个可访问标识符,可帮助您找到该控件。

iOS 11 AVPlayerViewController

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),
    ])
}

Result:结果:

播放速度按钮示例

As you may know, this has a lot of downsides because of all the edge cases that you will find, ie, you need to take care the landscape as well, you need to check if the hack is still valid on every new iOS version, etc. If you app supports iOS 10, then things are different because the UI of the AVPlayerViewController has changed in iOS 11.正如您可能知道的那样,这有很多缺点,因为您会发现所有边缘情况,即您还需要注意景观,您需要检查该 hack 是否在每个新的 iOS 版本上仍然有效,等等。如果你的应用程序支持 iOS 10,那么事情AVPlayerViewController一样了,因为AVPlayerViewController的 UI 在 iOS 11 中已经改变了。

  1. Is there a way we can refresh speed rate using an UISlider value change?有没有办法使用 UISlider 值更改来刷新速度?

Yes, instead of a UIButton for changing the playback speed, just add an UISlider .是的,而不是用于更改播放速度的UIButton ,只需添加一个UISlider

  1. Can we change the playback speed if it is playing currently?如果当前正在播放,我们可以更改播放速度吗?

Yes, if it's playing then you just need to change the rate property of the player: for example to 1.25 X => playerViewController.player?.rate = 1.25 .是的,如果它正在播放,那么您只需要更改播放器的rate属性:例如 1.25 X => playerViewController.player?.rate = 1.25

UPDATE:更新:

Enhancement requested!要求加强! 🙃 Radar number is 46022646: Playback speed control for AVPlayerViewController (Video) . 🙃 雷达编号为 46022646: AVPlayerViewController (Video) 的播放速度控制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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