AVPlayerItem videoComposition冻结IOS8

时间:2014-10-04 12:25:21

标签: ios8 avplayer freeze avmutablecomposition

我正在使用AVPlayer播放AVMutableVideoComposition,因为IOS8一切都很好。 但是现在视频开始播放,并且在4或5秒后它停止,如缓冲或类似的东西,声音继续播放,当视频结束AVPlayer循环并播放它没有停止。

我无法解决此问题。

任何帮助都会受到赞赏,

谢谢

3 个答案:

答案 0 :(得分:0)

同样在这里,不知道它是否可以算作答案,但无论如何,只需使用  [AVPlayer seekToTime:AVPlayer.currentItem.duration];自己做第一个循环并避免AVPlayer停止。这是我找到的唯一方式。

答案 1 :(得分:0)

我遇到了同样的问题,我这样解决了.. 而不是直接将videoComposition应用于AVPlayerItem ..我使用 AVAssetExportSession 导出我的视频以应用videoComposition,这反过来给了我一个带有我所有videoComposition应用的视频的Url,现在我可以使用这个contentURL来播放AVPlayer上的视频.. 这就像一个魅力.. 导出视频需要时间,但由于现在渲染视频合成不会在旅途中发生,它将顺利运行..

以下代码可用于导出视频..

AVAssetExportSession *export = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPreset1280x720];

export.videoComposition = videoComposition;
export.outputURL = [NSURL fileURLWithPath:[[NSTemporaryDirectory()  stringByAppendingPathComponent:[NSUUID new].UUIDString] stringByAppendingPathExtension:@"MOV"]];
export.outputFileType = AVFileTypeQuickTimeMovie;
export.shouldOptimizeForNetworkUse = YES;

[export exportAsynchronouslyWithCompletionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
  if (export.status == AVAssetExportSessionStatusCompleted) {

    completionHander(export.outputURL, nil);

  } else {

    completionHander(nil, export.error);

  }
});
}];

答案 2 :(得分:0)

我有同样的问题,但我得到了解决方案。

您应该在playerItem的状态更改为.ReadyToPlay后播放。

我也回答了here,该问题与您的问题类似。

请参阅以下内容。

func startVideoPlayer() {
    let playerItem = AVPlayerItem(asset: self.composition!)
    playerItem.videoComposition = self.videoComposition!

    let player = AVPlayer(playerItem: playerItem)
    player.actionAtItemEnd = .None

    videoPlayerLayer = AVPlayerLayer(player: player)
    videoPlayerLayer!.frame = self.bounds

    /* add playerItem's observer */
    player.addObserver(self, forKeyPath: "player.currentItem.status", options: .New, context: nil)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerItemDidReachEnd:", name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem);

    self.layer.addSublayer(videoPlayerLayer!)
}

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    if keyPath != nil && keyPath! == "player.currentItem.status" {
        if let newValue = change?[NSKeyValueChangeNewKey] {
            if AVPlayerStatus(rawValue: newValue as! Int) == .ReadyToPlay {
                playVideo() /* play after status is changed to .ReadyToPlay */
            }
        }
    } else {
        super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
    }
}    

func playerItemDidReachEnd(notification: NSNotification) {
    let playerItem = notification.object as! AVPlayerItem
    playerItem.seekToTime(kCMTimeZero)

    playVideo()
} 

func playVideo() {
    videoPlayerLayer?.player!.play()
}