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

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

问题描述

我已成功将音频文件保存到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")
         }
  }

推荐答案

检查官方

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")
        }
    }
}

我建议您阅读一些教程(例如,此处),您可能会在其中获得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?)

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

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