On tvOS if I want to allow play / pause but disable seeking, I can set avPlayerViewController.requiresLinearPlayback = YES
. However this property doesn't exist in iOS. How would I accomplish the same on iOS? I don't want to use avPlayerViewController.showsPlaybackControls = NO
because this would prevent play / pause. I only want to disable seeking. Furthermore, I'm not entirely sure that avPlayerViewController.showsPlaybackControls = NO
would even fully prevent seeking, as someone could plug in a headset with control buttons on it and press the physical "skip forward" button
I solved this problem by extending AVPlayer. Despite the Apple documentation saying that you should not extend AVPlayer and doing so has undefined behavior, in test everything worked fine on iOS 9 through 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
Now I can enable/disable seeking with just: player.seekingEnabled = (YES/NO);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.