iOS 9中Vine的无限视频循环(无缝视频)

时间:2016-08-20 21:14:59

标签: ios uiview uiviewcontroller video-streaming avplayer

我需要一些像Vine那样的无限视频循环的帮助。 我尝试了很多方法,所有方法都有短暂的延迟。

最受欢迎的一个:

  __weak typeof(self) weakSelf = self; // prevent memory cycle
  NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
 [noteCenter addObserverForName:AVPlayerItemDidPlayToEndTimeNotification
                    object:nil // any object can send
                     queue:nil // the queue of the sending
                usingBlock:^(NSNotification *note) {
                    // holding a pointer to avPlayer to reuse it
                    [weakSelf.avPlayer seekToTime:kCMTimeZero];
                    [weakSelf.avPlayer play];
                }];

有没有办法消除延迟并无缝播放本地视频?

我知道Apple在iOS 9中添加了一些更新,但我需要它在8+ ios上运行

对不起Ojc-C样本,我使用Swift

1 个答案:

答案 0 :(得分:0)

试试这个

-(void)startPlaybackForItemWithURL:(NSURL*)url {

// First create an AVPlayerItem
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:url];

// Subscribe to the AVPlayerItem's DidPlayToEndTime notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];

// Pass the AVPlayerItem to a new player
self.avPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem];

// Begin playback
[player play]

} 

-(void)itemDidFinishPlaying:(NSNotification *) notification {
    [self.avPlayer play];
    // Will be called when AVPlayer finishes playing playerItem
}