2016-07-28 60 views
1

我使用下面的代码从我的库中加载视频,然后播放它。我的问题是速度很慢,所以我必须等待重播约10秒,即使这样也不能正常工作。斯威夫特视频重播持续很长时间

func showVideo(obj: AnyObject!) { 
    manager.requestAVAssetForVideo(obj as! PHAsset, options: videoOptions, resultHandler: {(avAsset: AVAsset?, audioMix: AVAudioMix?, info: [NSObject : AnyObject]?) -> Void in 
     self.url = (avAsset as! AVURLAsset).URL 
     self.playVideo() 
    }) 
} 

// plays the video 
private func playVideo() { 
    asset = AVAsset(URL: url!) 
    playerItem = AVPlayerItem(asset: asset!) 

    player = AVPlayer(playerItem: self.playerItem!) 

    playerLayer = AVPlayerLayer(player: self.player) 
    playerLayer.frame = view.frame 
    view.layer.addSublayer(self.playerLayer!) 

    player!.play() 
} 

我从获取对象:

let allVideo = PHAsset.fetchAssetsWithMediaType(.Video, options: fetchOptions) 
showVideo(allVideo.lastObject) 

回答

0

要循环录像我使用的通知:

NSNotificationCenter.defaultCenter().addObserver(self 
    , selector: #selector(playerItemDidReachEnd(_:)) 
    , name: AVPlayerItemDidPlayToEndTimeNotification 
    , object: player.currentItem) 

和功能:

func playerItemDidReachEnd(notification: NSNotification) { 
    if let playerItem = notification.object { 
     playerItem.seekToTime(kCMTimeZero) 
    } 
} 
+0

和我在哪里应该添加NSNotificationCenter代码? – mafioso

+0

你可以把它放在播放视频(),但不要忘记删除观察者,如果你打算多次调用playVideo() – richy