How to perform some actions regarding the playback and pause actions of AVPlayer?

I used AVPlayerto play videos on my quick application. I want to place the play button (custom button) in the middle of the video. I know how to play and pause a video by clicking a button.

I could not find how to show / hide the user button when the video is playing / pausing with the default buttons? Is there any listener or something that works when playing / pausing a video?

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

+2
source share
1 answer

Here's how you can listen to player state changes and hide / show buttons: (suppose your player has a name playerand a button 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 the player’s speed is 0, this means that the video is not playing.

+1
source

Source: https://habr.com/ru/post/1618165/


All Articles