如何上传录制的音频文件进行解析?

how to upload recorded audio file to parse?

发布了我的代码 here, was getting clipped below

根据上面的代码,我在已知文件路径位置 soundfileURL 创建一个名为 sound.caf 的音频文件。

我的问题是如何将此文件上传到 Parse?如何从路径soundfileURL.

获取代码中的文件对象

我下面的尝试,显然需要工作。我没有 运行 因为我不知道如何将 sound.caf 转换为 PFFile 并上传到 Parse。

func addNamestoParse(obj: Music) {
    println("Doing Parse Stuff now")
    self.showActivityIndicatory(true)

    var className = PFObject(className: "musicrecording")
    className.setObject(obj.name, forKey: "nameofmusic")
    className.setObject(obj.createdOn, forKey: "datcreated")

如何在此处将 SOUND.CAF 文件转换为 PFFILE 并上传到 PARSE?

    className.saveInBackgroundWithBlock {
        (success: Bool, error: NSError?) -> Void in

        if success == true {



        } else {

            println(error)

        }
    }

}

感谢 David Jirman 的 Objective-C 等价物能够推导出 Swift 等价物。下面是代码。

func addNamestoParse(obj: Name) {
    println("Doing Parse Stuff now")

    var className = PFObject(className: "namerecordings")
    className.setObject(obj.name, forKey: "babyname")
    className.setObject(obj.nameSubmitter, forKey: "submittedby")
    className.saveInBackgroundWithBlock {
        (success: Bool, error: NSError?) -> Void in

        if success == true {
            let audioFile = PFFile(name: "mysound.caf", data: NSData(contentsOfURL: self.soundFileURL)!)
            className["audioFile"] = audioFile

            className.saveInBackgroundWithBlock{(success: Bool, error: NSError?) -> Void in

                if success == false {
                    println("error in uploading audio file")
                } else {
                    println("posted successfully")

                }
            }

        } else {

            println(error)

        }
    }

}

我对Swift不太熟悉,但在"old way"中原理应该是一样的。您需要创建一个 'PFFile' 并将其分配给 'musicrecording' class 的适当 PFFile 属性。例如:

PFFile *pfFile = [PFFile fileWithData:data];
PFObject *pfObject = [PFObject objectWithClassName:@"musicrecording"];
[pfObject setObject:pfFile forKey:@"musicFile"];
[pfObject saveInBackground];

保存包含 PFFile 对象的对象时,它会自动上传。这有帮助吗?

https://www.youtube.com/watch?v=4qj1piMAPE0 的这个很棒的教程之后,我的解决方案是 Swift:

    let path = getCacheDirectory().stringByAppendingPathComponent(fileName)
    let filePath = NSURL(fileURLWithPath: path)

    var dataToUpload : NSData = NSData(contentsOfURL: filePath!)!

    let soundFile = PFFile(name: fileName, data: dataToUpload)
    var userSound = PFObject(className:"upload")
    userSound["name"] = "Sara"
    userSound["sound"] = soundFile
    userSound.saveInBackground()

其中 getCacheDirectory() 是教程中使用的函数