问题
I'm having an issue with AVPlayer skipping the first 0.5 seconds of a video I'm playing when I attempt to play it immediately after pre-rolling.
First, I create the player:
self.videoPlayer = [[AVPlayer alloc] init];
Then, I add the player item to the player and add an observer to check and see when the item is ready:
[self.videoPlayer addObserver:self forKeyPath:@"currentItem" options:0 context:kBLCurrentPlayerItemStatusContext];
[self.videoPlayer replaceCurrentItemWithPlayerItem:self.currentVideoPlayerItem];
When the player item is observed, I will check to see if the player is ready to play. If it's not ready to play, I will add an observer to make sure it's ready to play:
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
...
} else if (context == kBLCurrentPlayerItemStatusContext) {
[self.videoPlayer removeObserver:self forKeyPath:@"currentItem" context:&kBLCurrentPlayerItemStatusContext];
if ([self.videoPlayer status]==AVPlayerStatusReadyToPlay) {
[self prerollVideoPlayer];
} else {
[self.videoPlayer addObserver:self forKeyPath:@"status" options:0 context:&kBLVideoPlayerStatusContext];
}
}
...
When the player status is observed as ready to play, I then preroll the player:
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
...
if (context == kBLVideoPlayerStatusContext) {
if ([self.videoPlayer status] == AVPlayerStatusReadyToPlay) {
[self.videoPlayer removeObserver:self forKeyPath:@"status" context:&kBLVideoPlayerStatusContext];
[self prerollVideoPlayer];
}
}
...
After the pre-roll is complete, I play the player:
-(void)prerollVideoPlayer{
[self.videoPlayer prerollAtRate:1.0 completionHandler:^(BOOL finished){
if (finished) {
[self.videoPlayer play];
}
}];
}
When the player plays in this way, it skips the first ~0.5 seconds of the video. If I don't have the player play immediately when it's finished pre-rolling, and I call the play method later it plays fine. I've checked to make sure all the observers are correctly observed and they occur in the order I've noted above. Is there another observer I need to add or a notification I should check for to avoid this skipping behavior?
来源:https://stackoverflow.com/questions/13977805/avplayer-skips-the-beginning-of-a-video