How to add custom button on AVPlayerViewController in Objective -C iOS?

3.3k Views Asked by At

I want to add a button in AVPlayerViewController but I am unable to do that. I added a button [AVPlayerViewController.contentOverlayView addSubview:_btnHandfree]; but it is not clickable, here is a screenshot

enter image description here

Maybe there is an upper layer in that unable to find.

3

There are 3 best solutions below

4
Athul Sethu On

Try this one. It may help.

#import "ViewController.h"
#import <AVKit/AVKit.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.


}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];


    NSURL *url=[[NSBundle mainBundle]URLForResource:@"videoplayback" withExtension:@"mov"];
    AVPlayer *player = [AVPlayer playerWithURL:url];
    AVPlayerViewController *playerViewController = [AVPlayerViewController new];
    playerViewController.player = player;

    UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake(20, 100, 100, 50)];
    [button addTarget:self
               action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown];

    [button setTitle:@"Button" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor whiteColor]];

    [self presentViewController:playerViewController animated:YES completion:^{
      [playerViewController.player play];
    }];
    [self.player addSubview:playerViewController.view];
    [self.view addSubview:button];

    for(UIWindow* tempWindow in [[UIApplication sharedApplication]windows]){
        for(UIView* tempView in [tempWindow subviews]){

            if ([[tempView description] rangeOfString:@"UIInputSetContainerView"].location != NSNotFound){
                [tempView addSubview:button];

                break;
            }
        }
    }
}

-(void)aMethod:(UIButton*)sender {
    NSLog(@"test");
}


@end
1
hariszaman On

As describe in Apple docs.

A view displayed between the video content and the playback controls.

You can debug view hierarchy to see where is the button in the stack.

So what if you add it to the view directly?

[AVPlayerViewController.view addSubview:yourButton];
0
nodebase On

If you look at the view controller's view's subviews, you will see its a single view called AVPlayerViewControllerContentView. You can do something like this when making your view controller:

func showFullScreenVideo(using player: AVPlayer) {
    // setup your player view controller
    let playerViewController = AVPlayerViewController()
    playerViewController.player = player
    present(playerViewController, animated: true) { [weak self] in
      self?.addShareButton(to: playerViewController)
    }
}

private func addShareButton(to playerViewController: AVPlayerViewController) {
    if let playerContentView = playerViewController.view.subviews.first {
        let someView = SomeView()
        playerContentView.addSubview(someView)
        // use your own layout code:
        someView.constrain(.centerX, .centerY, to: playerContentView)
    }
}

Adding the share button inside the completion block ensures that playerViewController.view is loaded and available to use. You can try to force the view to load in many ways. The choice is yours. Another possible way of doing this is adding this line of code just before adding your share button:

_ = playerViewController.view

instead of using the completion block. May or may not work, but the completion block method definitely works.