3

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.

rkyr
  • 3,131
  • 2
  • 23
  • 38
Narendra Ojha
  • 673
  • 8
  • 18
  • You can just set `playButton.enabled = false` and `playButton.alpha = 0` on the action. So when you click/tap the button, the other buttons (or that button as well) is disabled and it disappears. Make an `IBOutlet` out of that button however + an `IBAction`. @Narendra – Lukesivi Dec 02 '15 at 09:51
  • @Lukeslvi, Thanks for the reply. But I did not mean how to show/hide the button but what I cant get is how to trigger the IBAction when the default play/pause button is tapped? Because you can not drag the touch up inside action to the IBAction for default buttons. E.g. lets say I go to the video screen and click on "Play" button (default), the video starts playing and I want to hide the custom button. Then after some time, I click on "Pause" button (default), the video is paused and I want to show the custom button. Hope its easy to understand the question now. – Narendra Ojha Dec 02 '15 at 10:12

2 Answers2

3

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.

rkyr
  • 3,131
  • 2
  • 23
  • 38
  • 1
    I am working with AVPlayerController and add "rate" Observer. When i go offline at this situation the player pause. But it does not check "rate" observer method. – Sankalap Yaduraj Singh Sep 09 '16 at 07:38
  • @SankalapYadurajSingh, please create new question if you have one. Or try to google it. – rkyr Sep 09 '16 at 19:32
  • this my new question please visit. http://stackoverflow.com/questions/39411636/avplayerviewcontroller-play-pause-issue-in-ios?noredirect=1#comment66148154_39411636 – Sankalap Yaduraj Singh Sep 11 '16 at 05:25
0

player init then,

self.player?.addObserver(self, forKeyPath: "rate", options: .new, context: nil)

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if self.player?.rate == 0 {
        print("Pause")
    }else {
        print("Play")
    }
}
Atul Pol
  • 341
  • 3
  • 5