Search code examples
objective-ccustomizationmpmovieplayercontrolleravplayer

AVPlayerViewController with custom overlay


Migrating to AVkit from MPMoviePlayer has brought me to a blocking issue.

I need to display a custom tableView over the AVPlayerViewController. I can this tableview trough

[self.avVideoPlayer.contentOverlayView addsubiew:self.mycustomTableView]

and it is visible but it doesn't receive any tap/swipe events.

Any ideas why this happens or how can I add the table view in a place where it would receive the touch events even in the fullscreen mode?


Solution

  • [self.view addSubview:btn];
    

    This will add the button in the minimised player.

    Now for the fullscreen scenario you need to add an observer for the videoBounds, here is my code:

       [self.avVideoPlayer addObserver:self forKeyPath:@"videoBounds" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
    

    Now here comes the not so pretty part:

    - (void)observeValueForKeyPath: (NSString*) path
                      ofObject: (id)object
                        change: (NSDictionary*)change
                       context: (void*)context {
    
    NSLog(@"SOME OBSERVER DID THIS: %@ , %@",object,change);
    if ([self playerIsFullscreen]) {
        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!