2014-10-04 53 views
5

我正在用AVPlayer播放AVMutableVideoComposition,因爲IOS8一切都很完美。 但是現在視頻開始播放,4秒或5秒後停止播放,就像緩衝或類似的東西一樣,聲音不斷播放,當視頻播放結束時,AVPlayer會循環播放並播放。AVPlayerItem videoComposition freeze IOS8

我沒有解決此問題的線索。

任何幫助將感激,

謝謝

+1

我得到同樣的問題。可能是它與IOS 8的問題。 仍然我沒有得到任何解決方案..... !!! – 2015-01-20 12:11:10

回答

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() 
}  
0

同樣在這裏,不知道這是否可以指望像一個答案,但無論如何,只要使用 [AVPlayer seekToTime:AVPlayer.currentItem.duration];自己並做出第一循環避免AVPlayer停止。這是我發現的唯一方式。

0

我有同樣的問題,我解決了它這樣.. 代替將videoComposition直接AVPlayerItem ..我用AVAssetExportSession我的視頻應用videoComposition,這反過來給了我一個網址與視頻輸出的應用了我所有的videoComposition,現在我可以使用這個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); 

    } 
}); 
}];