如何从解析快速检索音频文件 [英] how to retrieve audio file from parse swift

查看:16
本文介绍了如何从解析快速检索音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地将一个音频文件保存到 Parse,但我正在努力再次下载它.我无法弄清楚 getDataInBackgroundWithBlock 应该使用什么块.以及如何实际保存文件.非常感谢任何帮助!

I have successfully saved an Audio file to Parse but am struggling to download it again. I can't work out what the block should be to go with getDataInBackgroundWithBlock. And how to actually save the file. Any help much appreciated!

let query = PFQuery(className: "wordRecordings")
query.whereKey("wordName", equalTo: "(joinedWord)")

query.findObjectsInBackgroundWithBlock { (objects:[PFObject]?, error:NSError?) -> Void in

    if error == nil{                        
       for object in objects! {
            let audioFile = object["audioFile"] as! PFFile
            audioFile.getDataInBackgroundWithBlock{ (audioFile: NSData, error:NSError?) -> Void in // THIS BLOCK ISNT CORRECT...
               if error == nil {
               let audioFile = AudioFile(data: audioFile) // THIS WORKS FOR IMAGES BUT NOT AUDIO - HOW DO I CREATE AN AUDIO FILE
               }
              }
             }
            } else {   
               print("Error: (error!) (error!.userInfo)")
           }
         }

感谢下面的@David,这里也提供了一个带有路径代码的可行解决方案.

Thanks to @David below here is a working solution with path code as well.

let fileString = "(fileName)"
let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let path = documentsDirectory.URLByAppendingPathComponent(fileString)

audioFile.getDataInBackgroundWithBlock { (audioFile:NSData?, error:NSError?) -> Void in
         if error == nil {
           audioFile.writeToURL(path, atomically: true)
         } else {
         print("error with getData")
         }
  }

推荐答案

查看官方文档 它应该是这样的(我不使用 Swift,所以请耐心等待):

Checking the official documentation it should look something like this (I don't use Swift so please bear with me):

audioFile.getDataInBackgroundWithBlock{ (audioFile: NSData?, error:NSError?) -> Void in
    if error == nil {
        let path = "path-to-your-file"
        if !audioFile.writeToFile(path, atomically: true){
            print("Error saving")
        }
    }
}

我建议您阅读一些教程(例如 此处a>) 在那里你可以获得更好的 Swift 文件处理示例.

I would recommend to go through some tutorials (e.g. here) where you might get better examples of file handling in Swift.

显然,块的参数都必须是可选的(参见 这里).你的结果块需要是 (audioFile: NSData?, error: NSError?)

Apparently both the block's parameters have to be optionals (see here). Your result block needs to be of type (audioFile: NSData?, error: NSError?)

这篇关于如何从解析快速检索音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆