首页 > 解决方案 > 如何在目标 c 中隐藏 AVPlayer 屏幕?

问题描述

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self
               action:@selector(playMethod)
     forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"Play" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor purpleColor] 
    forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
    [self.view addSubview:button];
}

- (void)playMethod {
    controller = [[AVPlayerViewController alloc] init];
    [self addChildViewController:controller];
    [self.view addSubview:controller.view];

    controller.view.frame = CGRectMake(15,50,345,300);
    controller.player = player;
    controller.showsPlaybackControls = YES;
    player.closedCaptionDisplayEnabled = NO;

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button1 addTarget:self
                action:@selector(backMethod)
      forControlEvents:UIControlEventTouchUpInside];
    [button1 setTitle:@"Back" forState:UIControlStateNormal];
    button1.frame = CGRectMake(50.0, 0.0, 160.0, 40.0); // set your own position
    [controller.view addSubview:button1];
    [player pause];
    [player play];


}

-(void)backMethod
{
    controller.view.hidden=YES;
    [player pause];
}

我正在尝试在 AVPlayer 中播放 RTMP URL,它工作正常,但问题是,我在播放器本身上创建了一个后退按钮,但是当我调用它的方法 (backMethod) 时没有任何反应。我想要,当我单击后退按钮时,它应该隐藏该屏幕(播放器屏幕)并且应该出现播放按钮屏幕。

标签: iosobjective-ciphonecocoapods

解决方案


尝试将您的播放器视图放在 viewController 视图的子视图中。

并隐藏您的子视图而不是隐藏 PlayerViewController 的视图,因为它是由 PlayerViewController 本身管理的。


推荐阅读