如何在iOS AVPlayerViewController上禁用搜索?

时间:2019-12-18 14:34:41

标签: ios avplayerviewcontroller

在tvOS上,如果我想允许播放/暂停但禁止搜索,则可以设置avPlayerViewController.requiresLinearPlayback = YES。但是,此属性在iOS中不存在。我将如何在iOS上完成相同的任务?我不想使用avPlayerViewController.showsPlaybackControls = NO,因为这会阻止播放/暂停。我 要禁用搜索。此外,我不确定avPlayerViewController.showsPlaybackControls = NO会完全阻止搜索,因为有人可以插入带有控制按钮的头戴式耳机并按下物理“向前跳过”按钮

1 个答案:

答案 0 :(得分:0)

我通过扩展AVPlayer解决了这个问题。尽管Apple文档说您不应该 扩展AVPlayer并且这样做具有不确定的行为,但是在测试中,一切在iOS 9到13上都可以正常工作

@interface CustomAVPlayer : AVPlayer

@property BOOL seekingEnabled;

@end

@implementation CustomAVPlayer

- (instancetype)initWithPlayerItem:(AVPlayerItem *)item
{
    self = [super initWithPlayerItem:item];
    if (self) {
        _seekingEnabled = YES;
    }
    return self;
}

- (void)seekToTime:(CMTime)time toleranceBefore:(CMTime)toleranceBefore toleranceAfter:(CMTime)toleranceAfter completionHandler:(void (^)(BOOL))completionHandler
{
    // This prevents seeking at the lowest level, regardless of whatever shenanigans the view may be doing
    if (_seekingEnabled)
        [super seekToTime:time toleranceBefore:toleranceBefore toleranceAfter:toleranceAfter completionHandler:completionHandler];
    else
        [super seekToTime:self.currentTime toleranceBefore:toleranceBefore toleranceAfter:toleranceAfter completionHandler:completionHandler];
}

@end

现在,我可以仅使用player.seekingEnabled = (YES/NO);

启用/禁用搜索