如何使用AVFoundation循环播放视频,而不会在结尾出现不必要的停顿? [英] How do I loop a video with AVFoundation without unwanted pauses at the end?

查看:138
本文介绍了如何使用AVFoundation循环播放视频,而不会在结尾出现不必要的停顿?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试播放无限循环的视频片段.我按照Apple的建议进行操作;通过设置由AVPlayerItemDidPlayToEndTimeNotification触发的通知:

I am trying to play a video clip that loops indefinitely. I am doing this the way Apple recommends; by setting up a notification that gets triggered by 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];
            }
        });
    }];
}

触发后,这将调用我的方法来有效地倒带并再次播放剪辑:

When triggered, this calls my method to effectively rewind and play the clip again:

-(void)videoPlayerItemDidReachEnd:(NSNotification *)notification
{
    [videoPlayerItem seekToTime:kCMTimeZero];
    [videoPlayer play];
}

问题是,每次使用此方法并循环回到开头时,视频播放都会出现短暂但可见的暂停.

The problem is, there is a brief but visible pause in the video playback every time it hits this method and loops back to the beginning.

视频剪辑为H.264,并且已经在其他播放器中进行了测试,以确保其内容中没有可见的跳过".在iOS6中,这一切都在模拟器中以及在iPad2和iPad3上进行.

The video clips are H.264 and have been tested in other players to ensure that they have no visible "skips" in their content. This is all happening under iOS6 both in the Simulator and on an iPad2 and iPad3.

我在做什么错了?

推荐答案

不幸的是,目前看来,使用AVPlayer实际上无法实现这一点-在每种情况下,开始在AVPlayer中播放时都会不可避免地出现打h.因此,使用多个AVPlayers等,信使"黑客似乎是解决此问题的唯一方法.

Unfortunately it appears this actually is not currently possible using AVPlayer as it stands- there is an unavoidable hiccup upon beginning playback in an AVPlayer in every case. So "messier" hacks appear to be the only way to solve this, using multiple AVPlayers, etc.

这篇关于如何使用AVFoundation循环播放视频,而不会在结尾出现不必要的停顿?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆