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
Ruby: Loaderror - Library Not Found for Class Digest::Sha1 -- Digest/Sha1
Uibezierpath Subclass Initializer
Creating a Rtsp Client for Live Audio and Video Broadcasting in Objective C
How to Change How a Remote Notification Is Presented Before Presentation
Delete Image from Photo Gallery
iOS - Setobject VS. Synchronize
Iad Interstitials Not Showing Consistently? and Not at All on the Simulator
iOS App Crashes on Phone But Works Fine on Simulator
How to Add Followed Text to Uitextfield
Nsexceptionallowsinsecurehttploads Not Working for Ip Addresses
Pure Swiftui Login, Signup, Register Flow, Is It Possible
Swift 3 - Passing Data Between a View Controller and After That to Another 2
Outline Uilabel Text in Uilabel Subclass
Why Does It Take Such a Long Time for UI to Be Updated from Background Thread
I Want to Switch Time Scrubber to Do 15 Min Intervals