簡體   English   中英

保存視頻以進行解析和播放

[英]Saving Video to Parse & Playback

所以我正在使用這個自定義類來錄制我的視頻-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()中的方法://為響應創建Player讓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 )粘貼到瀏覽器中,然后將其粘貼到瀏覽器中。 我是否正確假設文件已正確保存?

當我運行我的應用程序時,視頻無法播放。關於我做錯什么的任何建議?

我發現使用pfFile.url播放視頻不起作用。 您必須使用正確的擴展名(mov)將PFFIle中的NSData寫入本地文件,然后使用本地文件作為源來播放視頻。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM