swift - Play video downloaded through CloudKit as CKAsset - iOS -
i'm making app records video, uploads icloud using cloudkit ckasset, downloads file , plays in avplayer. written in swift 2.0
i have gotten data downloaded, , think i've been able reference i'm not sure. data/garbage print when convert url nsdata object , print console. video files gets downloaded binary file however. able go cloudkit dashboard , download file , append '.mov' it, , opened in quicktime no problem.
so think main issue can't work out how video file play, since file has no extension. have tried appending '.mov' end urlbyappendingpathextension() no avail. let me know of ideas!
upload video
func imagepickercontroller(picker: uiimagepickercontroller, didfinishpickingmediawithinfo info: [string : anyobject]) { let tempurl = info[uiimagepickercontrollermediaurl] as! nsurl dismissviewcontrolleranimated(true) { () -> void in self.uploadvideotoicloud(tempurl) print("\n before upload: \(tempurl)\n") } } func uploadvideotoicloud(url: nsurl) { let videorecord = ckrecord(recordtype: "video", recordid: id) videorecord["title"] = "this title" let videoasset = ckasset(fileurl: url) videorecord["video"] = videoasset ckcontainer.defaultcontainer().privateclouddatabase.saverecord(videorecord) { (record, error) -> void in dispatch_async(dispatch_get_main_queue(), { () -> void in if error == nil { print("upload successful") } else { print(error!) } }) } } download video
func downloadvideo(id: ckrecordid) { privatedatabase.fetchrecordwithid(id) { (results, error) -> void in dispatch_async(dispatch_get_main_queue()) { () -> void in if error != nil { print(" error fetching record " + error!.localizeddescription) } else { if results != nil { print("pulled record") let record = results! let videofile = record.objectforkey("video") as! ckasset self.videourl = videofile.fileurl print(" after download: \(self.videourl!)") self.videoasset = avasset(url: self.videourl!) self.playvideo() } else { print("results empty") } } } } }
the root problem avplayer expects file extension, example .mov, ckasset's fileurl property points file lacks extension. cleanest solution create hard link, avoids shuffling megabytes of data around , requires no disk space:
- (nsurl *)videourl { return [self createhardlinktovideofile]; } - (nsurl *)createhardlinktovideofile { nserror *err; if (![self.hardurl checkresourceisreachableandreturnerror:nil]) { if (![[nsfilemanager defaultmanager] linkitematurl:self.asset.fileurl tourl:self.hardurl error:&err]) { // if creating hard link failed still possible create copy of self.asset.fileurl , return url of copy } } return self.hardurl; } - (void)removehardlinktovideofile { nserror *err; if ([self.hardurl checkresourceisreachableandreturnerror:nil]) { if (![[nsfilemanager defaultmanager] removeitematurl:self.hardurl error:&err]) { } } } - (nsurl *)hardurl { return [self.asset.fileurl urlbyappendingpathextension:@"mov"]; } then in view controller, point avplayer videourl instead of asset.fileurl.
Comments
Post a Comment