Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Can't stream video from Parse Backend

Recently I have created my own parse server hosted on heroku using mongoLab to store my data.

My problem is I am saving a video as a parse PFFile, however I can not seem to be able to stream it after saving it.

Here are my exact steps.

First, I save the video returned by UIImagePicker

//Get the video URL
let videoURL = info[UIImagePickerControllerMediaURL] as? NSURL

//Create PFFile with NSData from URL
let data = NSData(contentsOfURL: videoURL!)
videoFile = PFFile(data: data!, contentType: "video/mp4")

//Save PFFile first, then save the PFUser
PFUser.currentUser()?.setObject(videoFile!, forKey: "profileVideo")
            videoFile?.saveInBackgroundWithBlock({ (succeeded, error) -> Void in
                print("saved video")
                PFUser.currentUser()?.saveInBackgroundWithBlock({ (succeeded, error) -> Void in
                    if succeeded && error == nil {
                        print("user saved")

                        //Hide progress bar 
                        UIView.animateWithDuration(0.5, animations: { () -> Void in                   
                            self.progressBar.alpha = 0
                            }, completion: { (bool) -> Void in
                                self.progressBar.removeFromSuperview()
                        })

                    }else{

                        //Show error if the save failed
                        let message = error!.localizedDescription
                        let alert = UIAlertController(title: "Uploading profile picture error!", message: message, preferredStyle: UIAlertControllerStyle.Alert)
                        let dismiss = UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil)
                        alert.addAction(dismiss)
                        self.presentViewController(alert, animated: true, completion: nil)

                    }
                })
                }, progressBlock: { (progress) -> Void in
                    self.progressBar.setProgress(Float(progress)/100, animated: true)
            })

This all works well. The problem lies when I retrieve the PFFile and try to stream the video. Here is my code for that:

//Get URL from my current user
self.videoFile = PFUser.currentUser()?.objectForKey("profileVideo") as? PFFile
                        self.profileVideoURL = NSURL(string: (self.videoFile?.url)!)

//Create AVPlayerController
let playerController = AVPlayerViewController()

//Set AVPlayer URL to where the file is stored on the sever
let avPlayer = AVPlayer(URL: self.profileVideoURL)
playerController.player = avPlayer

//Present the playerController
self.presentViewController(playerController, animated: true, completion: { () -> Void in
 playerController.player?.play()
})

What ends up happening when I present the playerController is this:

enter image description here

Why is this happening when I try stream my video?

Any help is greatly appreciated!

UPDATE

I have recently tried playing a video saved from a different database using this line of code: let videoURL = NSURL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")

This confirms that it is the format I am saving my PFFile in that is causing the error.

It must be this line causing the error as "video/mp4" is probably not the right format : videoFile = PFFile(data: data!, contentType: "video/mp4")

UPDATE 2

I have taken the direct link of my .mp4 file located on mongoLab and found I can play it in google chrome, but not on safari or my iPhone.

UPDATE 3

I have found that this was an issue with the parse api itself, and had nothing to do with the code as my code works perfectly when using the original parse backend (the one that is closing down) instead of my custom parse server. I currently have no solution however it should get fixed over time.

like image 766
Josh Avatar asked Feb 05 '16 23:02

Josh


1 Answers

this code works for me

let playerController = AVPlayerViewController()

      self.addChildViewController(playerController)
      self.view.addSubview(playerController.view)
      playerController.view.frame = self.view.frame

      file!.getDataInBackgroundWithBlock({
        (movieData: NSData?, error: NSError?) -> Void in
        if (error == nil) {

          let filemanager = NSFileManager.defaultManager()

          let documentsPath : AnyObject = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)[0]
          let destinationPath:NSString = documentsPath.stringByAppendingString("/file.mov")
          movieData!.writeToFile ( destinationPath as String, atomically:true)


          let playerItem = AVPlayerItem(asset: AVAsset(URL: NSURL(fileURLWithPath: destinationPath as String)))
          let player = AVPlayer(playerItem: playerItem)
          playerController.player = player
          player.play()
        } else {
          print ("error on getting movie data \(error?.localizedDescription)")
        }
      })
like image 50
lguerra10 Avatar answered Nov 13 '22 21:11

lguerra10