Draw Button on Top of Avplayer

Draw button on top of AVPlayer

You're using an AVPlayerViewController, so there's no reason to access your application's window like in Alessandro Ornano's answer. Why reinvent the wheel? Every AVPlayerViewController has a contentOverlayView property which allows you to place views between the player and the controls.

First, create a new AVPlayerItem and listen for the AVPlayerItemDidPlayToEndTimeNotification notification on that item. Load the item into your player and begin playback.

Once the item completes, the selector your specified to listen for the AVPlayerItemDidPlayToEndTimeNotification notification will be called. In that selector, access the contentOverlayView directly and add your buttons:

In some view controller or other object:

let playerVC = AVPlayerViewController()

// ...

func setupPlayer {

let playerItem = AVPlayerItem(...)
playerVC.player?.replaceCurrentItemWithPlayerItem(playerItem)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(VC.itemFinished), name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem)
self.presentViewController(playerVC, animated: true) {
self.playerVC.player?.play()
}
}

func itemFinished() {
let btn = UIButton(type: .System)
btn.addTarget(self, action: #selector(VC.buttonTapped), forControlEvents: .TouchUpInside)
self.playerVC.contentOverlayView?.addSubview(btn)
}

func buttonTapped() {
print("button was tapped")
// replay/comment logic here
}

As stated in the comments (and a rejected edit), buttons may not work in the contentOverlayView. For an alternate solution, see Pyro's answer.

You could also subclass AVPlayerViewController and do everything inside an instance of your subclass, but Apple warns against that:

Do not subclass AVPlayerViewController. Overriding this class’s methods is unsupported and results in undefined behavior.

Swift 2 AVPlayer - Play Next video Previous Video

I did create a demo project where it is shown how to put a custom buttons on top of AVPlayer and they responds to clicks.

What was done:

  • Xib (uses auto layout) file created which hold custom buttons like next, replay and others.
  • Buttons were connected to the source which prints what needs to do. Like: play next or replay.
  • Was added observer which observe when video is finished to play.
  • When video is finished to play xib file with buttons will be shown.

No Done button on AVPlayerViewController

Yes, the 'Done' button acts as a dismissal for a modal presentation style. If you are pushing the AVPlayerViewController onto the navigation stack you will need to handle the popping yourself.

Place AVPlayerViewController inside UIView

You can initialize the AVPlayerViewController and add it as a child to your ViewController , and insert it as a subview.

[self addChildViewController:yourPlayerViewController];
[self.view addSubview:yourPlayerViewController.view];
[yourPlayerViewController didMoveToParentViewController:self];

And to remove it:

[yourPlayerViewController willMoveToParentViewController:nil];
[yourPlayerViewController.view removeFromSuperview];
[yourPlayerViewController removeFromParentViewController];

EDIT SWIFT METHOD:
Check here for Swift



Related Topics



Leave a reply



Submit