AVPlayer在长时间延迟后开始重现视频

时间:2016-09-26 13:10:16

标签: ios objective-c avplayer ios10

延迟大约是10秒。 我的代码片段。它是HomeController的* .m文件:

@interface HomeController ()
@property(nonatomic, strong) AVPlayerViewController *playerViewController;
@implementation HomeController
- (void)viewDidLoad
{
    [super viewDidLoad];
    ...
    self.playerViewController = [[AVPlayerViewController alloc] init];
}
- (IBAction)watchDemoToggle:(id)sender {
    NSURL *url = [NSURL URLWithString:@"http://blahblahblah.com/demo.mp4"];
        AVURLAsset *asset = [AVURLAsset assetWithURL: url];
        AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
    if(playerItem)
    {
        AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
        self.playerViewController.player = player;
        [self.playerViewController.player addObserver:self forKeyPath:@"status" options:0 context:nil];
        [self.playerViewController setShowsPlaybackControls:NO];
        [self presentViewController:self.playerViewController animated:YES completion:^{
            [self.playerViewController.player play];
            self.playerViewController.showsPlaybackControls = YES;
        }];
    }
}

#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {
        if ([keyPath isEqualToString:@"status"]) {
            AVPlayerStatus status = [change[NSKeyValueChangeNewKey] integerValue];
            NSLog(@"status = %ld", (long)status);
        }
}

我添加了KVO并得到了点击播放器状态为AVPlayerStatusUnknown后的结果。之后,尽管视频正在复制,但它并没有改变。 有没有人有任何想法?

1 个答案:

答案 0 :(得分:3)

将AVPlayer的automaticWaitsToMinimizeStalling属性设置为false,以便立即开始播放。

AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
player.automaticallyWaitsToMinimizeStalling = false;

但如果没有足够的内容可供播放,那么播放器可能会失速。

有关详细信息,请参阅此Apple文档。

avplayer