Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. //Get the video URL
  2. let videoURL = info[UIImagePickerControllerMediaURL] as? NSURL
  3.  
  4. //Create PFFile with NSData from URL
  5. let data = NSData(contentsOfURL: videoURL!)
  6. videoFile = PFFile(data: data!, contentType: "video/mp4")
  7.  
  8. //Save PFFile first, then save the PFUser
  9. PFUser.currentUser()?.setObject(videoFile!, forKey: "profileVideo")
  10. videoFile?.saveInBackgroundWithBlock({ (succeeded, error) -> Void in
  11. print("saved video")
  12. PFUser.currentUser()?.saveInBackgroundWithBlock({ (succeeded, error) -> Void in
  13. if succeeded && error == nil {
  14. print("user saved")
  15.  
  16. //Hide progress bar
  17. UIView.animateWithDuration(0.5, animations: { () -> Void in
  18. self.progressBar.alpha = 0
  19. }, completion: { (bool) -> Void in
  20. self.progressBar.removeFromSuperview()
  21. })
  22.  
  23. }else{
  24.  
  25. //Show error if the save failed
  26. let message = error!.localizedDescription
  27. let alert = UIAlertController(title: "Uploading profile picture error!", message: message, preferredStyle: UIAlertControllerStyle.Alert)
  28. let dismiss = UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil)
  29. alert.addAction(dismiss)
  30. self.presentViewController(alert, animated: true, completion: nil)
  31.  
  32. }
  33. })
  34. }, progressBlock: { (progress) -> Void in
  35. self.progressBar.setProgress(Float(progress)/100, animated: true)
  36. })
  37.  
  38. //Get URL from my current user
  39. self.videoFile = PFUser.currentUser()?.objectForKey("profileVideo") as? PFFile
  40. self.profileVideoURL = NSURL(string: (self.videoFile?.url)!)
  41.  
  42. //Create AVPlayerController
  43. let playerController = AVPlayerViewController()
  44.  
  45. //Set AVPlayer URL to where the file is stored on the sever
  46. let avPlayer = AVPlayer(URL: self.profileVideoURL)
  47. playerController.player = avPlayer
  48.  
  49. //Present the playerController
  50. self.presentViewController(playerController, animated: true, completion: { () -> Void in
  51. playerController.player?.play()
  52. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement