所以我正在使用这个自定义类来录制我的视频——https://github.com/piemonte/PBJVision。我正在尝试在我的 iOS 应用程序中录制视频,但我似乎无法正确获取代码以将文件上传到我的解析服务器。一些东西:
在 PBJVision 类中,它允许您在录制视频后使用 NSURL(fileWithPath:videoPath) 访问资产。
要访问资产中的数据并保存到 Parse,我使用以下函数:
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() } } } }
}
然后我使用 UITableView 显示来自 Parse 的数据。下面是我如何从 Parse 中检索我的资产并进入我的 AVPlayer(): // 为反应创建播放器 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()
我复制从 urlFromParse 返回的值(http://parlayapp.herokuapp.com/parse/files/smTrXDGZhlYQGh4BZcVvmZ2rYB9kA5EhPkGbj2R2/58c0648ae4ca9900f2d835feb77f165e_file.mp4)并将其粘贴到我的浏览器中,视频在浏览器中播放。我是否正确假设文件已正确保存?
当我去运行我的应用程序时,视频不播放。关于我做错了什么有什么建议吗?