AVPlayerViewController.ContentOverlayView 中的自定义按钮

Mtn*_*ger 5 objective-c ios ios8

我需要向 AVPlayerViewController 添加一个自定义按钮,该按钮将显示在运行 iOS 8 的应用程序的全屏和非全屏中。

将按钮添加到 AVPlayerViewController.view 或包含视图将适用于非全屏,但当播放器切换到全屏时,该按钮不再可见。我发现如果我向 AVPlayerViewController.ContentOverlayView 添加一个按钮,那么它会出现在全屏和非全屏模式中,但是 ContentOverlayView 似乎不会响应任何触摸,因此无法单击该按钮。有谁知道添加按钮的不同位置或使 ContentOverlayView 响应触摸的方法?

示例代码

AVPlayerViewController *playerView = [[AVPlayerViewController alloc] init];
playerView.player = [AVPlayer playerWithURL:movieURL];
CGRect viewInsetRect = CGRectInset ([self.view bounds],
                                    kMovieViewOffsetX,
                                    kMovieViewOffsetY );
/* Inset the movie frame in the parent view frame. */
[[playerView view] setFrame:viewInsetRect];
[self.view addSubview: [playerView view]];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = [UIColor yellowColor];
btn.frame = CGRectMake(50, 50, 200, 75);
[btn addTarget:self action:@selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[btn setUserInteractionEnabled:YES];
[btn setEnabled:YES];

[playerView.contentOverlayView addSubview:btn];
Run Code Online (Sandbox Code Playgroud)

Dur*_*rdu 0

我刚才也遇到了同样的困境。

代替:

[playerView.contentOverlayView addSubview:btn];
Run Code Online (Sandbox Code Playgroud)

使用(添加playerView之后)

[self.view addSubview:btn];
Run Code Online (Sandbox Code Playgroud)

这将在最小化的播放器中添加按钮。

现在,对于全屏,我没有找到任何其他可靠的方法来检查播放器是否进入全屏或返回,所以这里有一个解决方案:

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

和这个:

-(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;
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道这不是一个好方法,但这是我设法添加一些自定义 UI 并接收播放器上的触摸事件的唯一方法。

干杯!