Created
July 9, 2015 13:22
-
-
Save mingsai/a59516ba362bf65949a7 to your computer and use it in GitHub Desktop.
This controller is used to manage video playback of a video stored as binary data within Core Data. It solves the problem by saving a file into the temporary directory and sending it to an AVPlayerViewController that is within a container view of the VideoPlayerViewController.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // VideoPlayerViewController.swift | |
| // | |
| // | |
| // Created by Tommie Carter on 7/8/15. | |
| // Copyright © 2015 MING Technology. All rights reserved. | |
| // | |
| import UIKit | |
| import AVKit | |
| import AVFoundation | |
| class VideoPlayerViewController: UIViewController,AVPlayerItemMetadataOutputPushDelegate { | |
| var assetTrack:AVAssetTrack? | |
| var playerItem:AVPlayerItem? | |
| var player: AVPlayer? | |
| var story:VideoStory? { | |
| didSet { | |
| setupVideo() | |
| } | |
| } | |
| @IBOutlet weak var container: UIView! | |
| struct Constants { | |
| static let AVPlayerStatusObservationContext = UnsafeMutablePointer<Void>() | |
| } | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| //self.player?.play() | |
| // Do any additional setup after loading the view. | |
| } | |
| override func viewDidDisappear(animated: Bool) { | |
| super.viewDidDisappear(animated) | |
| } | |
| override func viewWillDisappear(animated: Bool) { | |
| super.viewWillDisappear(animated) | |
| self.playerItem?.removeObserver(self, forKeyPath: "status") | |
| } | |
| override func didReceiveMemoryWarning() { | |
| super.didReceiveMemoryWarning() | |
| // Dispose of any resources that can be recreated. | |
| } | |
| /* | |
| // MARK: - Navigation | |
| // In a storyboard-based application, you will often want to do a little preparation before navigation | |
| override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { | |
| // Get the new view controller using segue.destinationViewController. | |
| // Pass the selected object to the new view controller. | |
| } | |
| */ | |
| func setupVideo () { | |
| let filePath = NSTemporaryDirectory() + "playbackVideo.mov" | |
| let tmpURL = NSURL( fileURLWithPath: filePath ) | |
| if let _ = self.story?.data?.writeToURL(tmpURL, atomically: true) { | |
| if NSFileManager.defaultManager().fileExistsAtPath( filePath ) { | |
| let asset = AVAsset(URL: tmpURL) | |
| asset.loadValuesAsynchronouslyForKeys(["tracks","duration"], completionHandler: { () -> Void in | |
| let videoTracks = asset.tracksWithMediaType(AVMediaTypeVideo) | |
| self.assetTrack = videoTracks.first | |
| if (self.assetTrack?.enabled == nil) { | |
| print("asset track not ready") | |
| } | |
| }) | |
| self.playerItem = AVPlayerItem(asset: asset) | |
| let output = AVPlayerItemMetadataOutput(identifiers: nil) | |
| output.setDelegate(self, queue: dispatch_get_main_queue()) | |
| playerItem?.addOutput(output) | |
| self.playerItem?.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions.New, context: Constants.AVPlayerStatusObservationContext) | |
| self.player = AVPlayer(playerItem: self.playerItem!) | |
| } | |
| } | |
| } | |
| override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) { | |
| // | |
| if context == Constants.AVPlayerStatusObservationContext { | |
| if (keyPath! == "status") { | |
| if (player!.status == AVPlayerStatus.ReadyToPlay) { | |
| print("ready") | |
| readyToPlay() | |
| } else if (player!.status == AVPlayerStatus.Failed) { | |
| // something went wrong. player.error should contain some information | |
| } else if (player!.status == AVPlayerStatus.Unknown) { | |
| print("unknown") | |
| } | |
| } | |
| } | |
| } | |
| func readyToPlay() { | |
| if let avpVC = self.childViewControllers.first as? AVPlayerViewController { | |
| dispatch_async(dispatch_get_main_queue()) { | |
| avpVC.preferredContentSize = CGSizeMake(self.container.bounds.width, self.container.bounds.height) | |
| avpVC.player = self.player | |
| avpVC.player?.play() | |
| } | |
| } | |
| } | |
| } | |
Author
Sorry about the late reply. VideoStory is just a subclass of NSManagedObject.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! I wanted to try playing around with this to learn how to playback video. I downloaded but unfortunately am not sure what 'VideoStory' is as you supplied only this file and not the entire project. Care to elaborate on this? Thanks