我正在尝试播放无限循环的视频剪辑。我按照 Apple 推荐的方式进行操作;通过设置由 AVPlayerItemDidPlayToEndTimeNotification 触发的通知:
@property(nonatomic) AVPlayer *videoPlayer;
@property(nonatomic) AVPlayerItem *videoPlayerItem;
-(void)loadVideo
{
NSURL *url = [[NSBundle mainBundle] URLForResource:filename withExtension:extension];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
NSString *tracksKey = @"tracks";
[asset loadValuesAsynchronouslyForKeys:@[tracksKey] completionHandler:
^{
dispatch_async(dispatch_get_main_queue(),
^{
NSError *error;
AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey error:&error];
if (status == AVKeyValueStatusLoaded)
{
[self setVideoPlayerItem:[AVPlayerItem playerItemWithAsset:asset]];
[videoPlayerItem addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusContext];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector (videoPlayerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:videoPlayerItem];
[self setVideoPlayer:[AVPlayer playerWithPlayerItem:videoPlayerItem]];
[scenePlayerView setVideoPlayer:videoPlayer];
}
});
}];
}
触发时,这会调用我的方法来有效地倒带并再次播放剪辑:
-(void)videoPlayerItemDidReachEnd:(NSNotification *)notification
{
[videoPlayerItem seekToTime:kCMTimeZero];
[videoPlayer play];
}
问题是,每次播放此方法并循环回到开头时,视频播放都会出现短暂但可见的暂停。
视频剪辑是 H.264 并且已经在其他播放器中进行了测试,以确保它们的内容中没有可见的“跳过”。这一切都发生在 iOS6 下的模拟器和 iPad2 和 iPad3 上。
我究竟做错了什么?