So i'm using this custom class to record my video -- https://github.com/piemonte/PBJVision. I am attempting to record video in my iOS app and I can't seem to get the code correct to upload the file to my parse server. A few things:
In the PBJVision class it allows you to use NSURL(fileWithPath:videoPath) to access the asset after the video has been recorded.
To access the Data in the asset and save to Parse, I use the following function:
func vision(vision: PBJVision, capturedVideo videoDict: [NSObject : AnyObject]?, error: NSError?) { if error != nil { print("Encountered error with video") isVideo = false } else { let currentVideo = videoDict let videoPath = currentVideo![PBJVisionVideoPathKey] as! String print("The video path is: \(videoPath)") self.player = Player() self.player.delegate = self self.player.view.frame = CGRect(x: cameraView.frame.origin.x, y: cameraView.frame.origin.y, width: cameraView.frame.width, height: cameraView.frame.height) self.player.playbackLoops = true videoUrl = NSURL(fileURLWithPath: videoPath) self.player.setUrl(videoUrl) self.cameraView.addSubview(self.player.view) self.player.playFromBeginning() nextButton.hidden = false isVideo = true let contents: NSData? do { contents = try NSData(contentsOfFile: videoPath, options: NSDataReadingOptions.DataReadingMappedAlways) } catch _ { contents = nil } print(contents) let videoObject = PFObject(className: "EventChatroomMessages") videoObject.setValue(user, forKey: "user") videoObject.setValue("uG7v2KWBQm", forKey: "eventId") videoObject.setValue(NSDate(), forKey: "timestamp") let videoFile: PFFile? do { videoFile = try PFFile(name: randomAlphaNumericString(26) + ".mp4", data: contents!, contentType: "video/mp4") print("VideoFile: \(videoFile)") } catch _ { print("error") } print(videoFile) videoObject.setValue(videoFile, forKey: "image") videoObject.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in if success == true { ProgressHUD.showSuccess("Video Saved.", interaction: false) dispatch_async(dispatch_get_main_queue()) { ProgressHUD.dismiss() } } else { ProgressHUD.showError("Error Saving Video.", interaction: false) dispatch_async(dispatch_get_main_queue()) { ProgressHUD.dismiss() } } } }
}
I am then using a UITableView to display my data from Parse. Here is how I retrieve my asset back from Parse and into my AVPlayer(): // Create Player for Reaction let player = Player() player.delegate = self player.view.frame = CGRectMake(0.0, nameLabel.frame.origin.y + nameLabel.frame.size.height + 0.0, self.view.frame.width, 150) player.view.backgroundColor = UIColor.whiteColor()
let video = message.objectForKey("image") as! PFFile
let urlFromParse = video.url!
print(urlFromParse)
let url = NSURL(fileURLWithPath: video.url!)
print(url)
let playerNew = AVPlayer(URL: url!)
let playerLayer = AVPlayerLayer(player: playerNew)
playerLayer.frame = CGRectMake(0.0, nameLabel.frame.origin.y + nameLabel.frame.size.height + 0.0, self.view.frame.width, 150)
cell.layer.addSublayer(playerLayer)
playerLayer.backgroundColor = UIColor.whiteColor().CGColor
playerNew.play()
I copy the value that is returned from urlFromParse which is (http://parlayapp.herokuapp.com/parse/files/smTrXDGZhlYQGh4BZcVvmZ2rYB9kA5EhPkGbj2R2/58c0648ae4ca9900f2d835feb77f165e_file.mp4) and paste it into my browser and the video plays in browser. Am I correct to assume the file has been saved correctly?
When I go to run my app, the video does not play.Any suggestion on what i'm doing wrong?