From 6cb880d4ae486fb75087f4d8438823edacd5e489 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Tue, 10 Nov 2015 15:26:01 +0800 Subject: [PATCH] video play --- .../MediaPreviewViewController.swift | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/Yep/ViewControllers/MediaPreview/MediaPreviewViewController.swift b/Yep/ViewControllers/MediaPreview/MediaPreviewViewController.swift index 8a0cc4be..eb3c4d47 100644 --- a/Yep/ViewControllers/MediaPreview/MediaPreviewViewController.swift +++ b/Yep/ViewControllers/MediaPreview/MediaPreviewViewController.swift @@ -168,6 +168,48 @@ class MediaPreviewViewController: UIViewController { } }) } + + override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer) { + + if let player = object as? AVPlayer { + + let indexPath = NSIndexPath(forItem: currentIndex, inSection: 0) + guard let cell = mediasCollectionView.cellForItemAtIndexPath(indexPath) as? MediaViewCell else { + return + } + + if player == cell.mediaView.videoPlayerLayer.player { + + if keyPath == "status" { + switch player.status { + + case AVPlayerStatus.Failed: + println("Failed") + + case AVPlayerStatus.ReadyToPlay: + println("ReadyToPlay") + dispatch_async(dispatch_get_main_queue()) { + cell.mediaView.videoPlayerLayer.player?.play() + + cell.mediaView.videoPlayerLayer.hidden = false + cell.mediaView.scrollView.hidden = true + } + + case AVPlayerStatus.Unknown: + println("Unknown") + } + } + } + } + } + + func playerItemDidReachEnd(notification: NSNotification) { + mediaControlView.playState = .Pause + + if let playerItem = notification.object as? AVPlayerItem { + playerItem.seekToTime(kCMTimeZero) + } + } } // MARK: - UICollectionViewDataSource, UICollectionViewDelegate