gpt4 book ai didi

swift - 保存视频以解析和播放

转载 作者:搜寻专家 更新时间:2023-11-01 05:36:52 24 4
gpt4 key购买 nike

所以我正在使用这个自定义类来录制我的视频 -- https://github.com/piemonte/PBJVision .我试图在我的 iOS 应用程序中录制视频,但我似乎无法获得正确的代码来将文件上传到我的解析服务器。一些事情:

  • 在 PBJVision 类中,它允许您在录制视频后使用 NSURL(fileWithPath:videoPath) 访问 Assets 。

  • 要访问 Assets 中的数据并保存到 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 中检索我的 Assets 并进入我的 AVPlayer(): //为 react 创建玩家 让玩家=玩家() 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 写入本地文件,然后使用本地文件作为源播放视频。

关于swift - 保存视频以解析和播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36529724/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com