【问题标题】:How to disable the play pause button in AVPlayerViewController on tvOS?如何在 tvOS 上禁用 AVPlayerViewController 中的播放暂停按钮?
【发布时间】:2016-08-17 16:38:12
【问题描述】:

我的 AVPlayerViewController 中有一个实时视频,我想禁用播放暂停按钮。我该怎么做?

我试过了,但它不起作用:

UITapGestureRecognizer *playPauseRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:nil];  
playPauseRec.allowedPressTypes = @[@(UIPressTypePlayPause)];
[self.avPlayerViewController.view addGestureRecognizer:playPauseRec ];

AVPlayerViewController 是 View Controller 的子视图控制器。

【问题讨论】:

    标签: objective-c tvos avplayerviewcontroller


    【解决方案1】:

    对该按钮的操作调用不同的方法。我通过在手势动作上不调用任何内容来做到这一点。这是我的代码

    UITapGestureRecognizer *tapGestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(PlayPause)];
    tapGestureRec.allowedPressTypes = @[@(UIPressTypePlayPause)];
    [self.view addGestureRecognizer:tapGestureRec];   
    

    并在 playPause 功能

    -(void)PlayPause
      {
     NSLog(@"Do Anything or Nothing");
     }
    

    【讨论】:

      【解决方案2】:

      如果您想禁用所有事件(播放/暂停/搜索),您可以通过将 AVPlayerViewControllerisUserInteractionEnabled 标志设置为 false 来实现。

          let playerViewController = AVPlayerViewController()
          // ... setup playerViewController here
      
          // disable userinteraction - so no play/pause/seek events are triggered anymore
          playerViewController.view.isUserInteractionEnabled = false
      

      【讨论】:

        【解决方案3】:

        如果您愿意使用私有 API 并希望获得更多控制权,您可以在 AVPlayerViewController 上设置私有委托。委托提供了一些特殊的回调,其中包括允许您完全禁用暂停。

        @objc protocol YourPrivateAVPlayerViewControllerDelegate {
          @objc optional func playerViewController(_ controller: AVPlayerViewController, shouldPlayFromTime: TimeInterval, completion: @escaping (Bool) -> Void)
          @objc optional func playerViewController(_ controller: AVPlayerViewController, shouldPauseWithCompletion: @escaping (Bool) -> Void)
        }
        

        您可以实现第二个委托方法并调用 shouldPauseWithCompletion(false) 来禁用暂停。如果您想根据流的类型或其他一些属性禁用暂停,当然也可以这样做。

        在 AVPlayerViewController 上设置委托实现:

        playerController.perform(Selector(("setPrivateDelegate:")), with: self)
        

        【讨论】:

          猜你喜欢
          • 2016-02-23
          • 1970-01-01
          • 2017-02-02
          • 1970-01-01
          • 2020-04-12
          • 1970-01-01
          • 2019-05-29
          • 2014-03-08
          • 1970-01-01
          相关资源
          最近更新 更多