Posted my code here, was getting clipped below.
As per the code above, I am creating an audio file called sound.caf
at a known filepath location soundfileURL
.
My question is how can I upload this file to Parse? How to get the file object in the code from the path soundfileURL
.
My attempt below, needs work obviously. I haven't run it because i dont know how to convert sound.caf
to PFFile and upload to 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")
HOW TO CONVERT SOUND.CAF FILE TO PFFILE HERE & UPLOAD TO PARSE??
className.saveInBackgroundWithBlock {
(success: Bool, error: NSError?) -> Void in
if success == true {
} else {
println(error)
}
}
}
Thanks to David Jirman for the Objective-C equivalent was able to deduce the Swift Equivalent. Below is the code.
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)
}
}
}
After this great tutorial at https://www.youtube.com/watch?v=4qj1piMAPE0 my solution was in 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()
where getCacheDirectory() is a function used in the tutorial
I'm not well versed with Swift but in the "old way" the principle should be the same. You need to create a 'PFFile' and assign that to the appropriate PFFile attribute of your 'musicrecording' class. For example:
PFFile *pfFile = [PFFile fileWithData:data];
PFObject *pfObject = [PFObject objectWithClassName:@"musicrecording"];
[pfObject setObject:pfFile forKey:@"musicFile"];
[pfObject saveInBackground];
When saving the object containing the PFFile object it uploads automatically. Does this help?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With