热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何从parseswift中检索音频文件?-howtoretrieveaudiofilefromparseswift

IhavesuccessfullysavedanAudiofiletoParsebutamstrugglingtodownloaditagain.Icantwor

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!

我已经成功地保存了一个音频文件来解析,但是我还在努力下载它。我无法计算出该块应该与getDataInBackgroundWithBlock一起使用。以及如何保存文件。任何帮助表示感谢!

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

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

感谢下面的@David,这里还有一个带有路径代码的有效解决方案。

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

1 个解决方案

#1


2  

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

查看官方文件应该是这样的(我没有使用Swift,请耐心等待):

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

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

我建议您通过一些教程(例如这里),在那里您可能会得到更快的文件处理示例。

Update

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

显然,块的两个参数都必须是选项(参见这里)。您的结果块需要类型(audioFile: NSData?错误:NSError ?)


推荐阅读
author-avatar
小白秋波_314
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有