2

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?

  • can you get a local file to play in your app just to make sure that part of your code is fine ? – DogCoffee Apr 10 '16 at 12:32
  • I used a link to test display media in the table and it worked. I used the following link: let videoUrl = NSURL(string: "https://v.cdn.vine.co/r/videos/AA3C120C521177175800441692160_38f2cbd1ffb.1.5.13763579289575020226.mp4")! – Michael Westbrooks II Apr 10 '16 at 12:38
  • So the actual playing of a video is fine (as seen in test), and the link you obtain from parse works when you paste in a browser... tricky one. PS you parse is going away in a few months ? – DogCoffee Apr 11 '16 at 01:18

1 Answers1

0

I have found that playing video using the pfFile.url does not work. You have to write the NSData from the PFFIle to a local file using the right extension (mov) and then play the video using the local file as the source.

lguerra10
  • 249
  • 3
  • 5