How to perform some action on Play and Pause actions of AVPlayer?

久未见 提交于 2019-12-05 02:07:49

问题


I have used AVPlayer to play video on my swift application. I want to place a Play button (custom button) at the middle of video. I know how to play and pause video on tapping the button.

I couldn't find how to show/hide the custom button when the video is played/paused from default buttons? Are there any listener or something which triggers when the video is played/paused?

I also want to hide the "Audio & Subtitles" icon from default control.


回答1:


Here is how you can listen to player state changes and hide/show buttons: (assume your player is named player, and button is playButton)

player.addObserver(self, forKeyPath: "rate", options: .New, context: nil) // somewhere after player init
...
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
  if keyPath == "rate" {
    playButton.enabled = player.rate == 1 
  } else {
    super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
  }
}

If rate of player is equal to 0 it means that video is not playing.



来源:https://stackoverflow.com/questions/34038368/how-to-perform-some-action-on-play-and-pause-actions-of-avplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!