Custom button in AVPlayerViewController.ContentOverlayView

自古美人都是妖i 提交于 2019-12-06 03:38:43

I managed to get this working by adding the following to my AVPlayerViewController in IB:

  1. An overlay view with a button inside it
  2. Add constraints to center the button in the overlay view
  3. Created an IBOutlet to the view (I called it 'overlay')
  4. Created an action from the button to AVPlayerViewController that prints something so that we can see that tapping the button is working
  5. Dragged the overlay view out of the view hierarchy and onto the top bar of the view controller (appears as a view icon in top bar of the view controller along with the exit icon etc)

and then in the code for the AVPlayerViewController:

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.addSubview(self.overlay)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        self.overlay.frame = self.view.bounds
    }

    @IBAction func playBtnTapped(sender: AnyObject) {
        println("PlayBtnTapped")
    }

The button is now centered in fullscreen/normal viewing and the button works

I had the same predicament just now.

Instead of:

[playerView.contentOverlayView addSubview:btn];

use (after you add the playerView)

[self.view addSubview:btn];

This will add the button in the minimised player.

Now for the fullscreen I didn't found any other reliable method of checking if the player went fullscreen or back, so here is a a solution:

self.windowChecker = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(checkIfFullscreen) userInfo:nil repeats:YES];

and this:

-(void)checkIfFullscreen{
    NSLog(@"videoBounds\n %f %f ", self.avVideoPlayer.contentOverlayView.frame.size.width,self.avVideoPlayer.contentOverlayView.frame.size.height);

    if ([self playerIsFullscreen]) { // check the frame on this
        for(UIWindow* tempWindow in [[UIApplication sharedApplication]windows]){
            for(UIView* tempView in [tempWindow subviews]){

                if ([[tempView description] rangeOfString:@"UIInputSetContainerView"].location != NSNotFound){
                    UIButton *testB = [[UIButton alloc] initWithFrame: CGRectMake(20, 20, 400, 400)];
                    testB.backgroundColor = [UIColor redColor];
                    [testB addTarget:self action:@selector(buttonTouch) forControlEvents:UIControlEventTouchDown];
                    [tempView addSubview:testB];

                    break;
                }
            }
        }
    }
}

I know this is a not a nice way to do it, but it is the only way I managed to add some custom UI that also receives touch events on the player.

Cheers!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!