我的AVPlayerViewController中有一个实时视频,我想禁用播放暂停按钮。我怎样才能做到这一点?
我试过这个,但它不起作用:
UITapGestureRecognizer *playPauseRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:nil];
playPauseRec.allowedPressTypes = @[@(UIPressTypePlayPause)];
[self.avPlayerViewController.view addGestureRecognizer:playPauseRec ];
AVPlayerViewController是View Controller的子视图控制器。
在该按钮的操作上调用不同的方法。我通过在手势行动上不做任何调用来做到这一点。这是我的代码
UITapGestureRecognizer *tapGestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(PlayPause)];
tapGestureRec.allowedPressTypes = @[@(UIPressTypePlayPause)];
[self.view addGestureRecognizer:tapGestureRec];
并在playPause功能
-(void)PlayPause
{
NSLog(@"Do Anything or Nothing");
}
如果您愿意使用私有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)
如果要禁用所有事件(播放/暂停/搜索),可以通过将isUserInteractionEnabled
的AVPlayerViewController
标志设置为false
来实现。
let playerViewController = AVPlayerViewController()
// ... setup playerViewController here
// disable userinteraction - so no play/pause/seek events are triggered anymore
playerViewController.view.isUserInteractionEnabled = false