i trying download youtube video in app , used this , library included in answer app crashing error :
fatal error: unexpectedly found nil while unwrapping optional value
here swift code:
import uikit class viewcontroller: uiviewcontroller { let videoid = "hs5cfp8n_js" var extractor = lbyoutubeextractor() override func viewdidload() { super.viewdidload() } @ibaction func start(sender: anyobject) { var url: string = "https://www.youtube.com/watch?v=hs5cfp8n_js" let testurl = nsurl(string:url) if testurl != nil { extractor = lbyoutubeextractor(url: testurl, quality: lbyoutubevideoqualitylarge) extractor.extractvideourlwithcompletionblock({(videourl, error) in if error == nil { println(videourl) dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), { let data = nsdata(contentsofurl: videourl)! let pathtodocs = nssearchpathfordirectoriesindomains(.documentdirectory, .userdomainmask, true)[0] as! string let filename = "video_\(self.videoid).mp4" let yourpath = pathtodocs.stringbyappendingpathcomponent(filename) data.writetofile(yourpath, atomically: true) println("file \(filename) saved") }) } else { println("failed extracting video url using block due error:\(error)") } }) } } } my project crashing @ line:
let data = nsdata(contentsofurl: videourl)! but can videourl in console:
http%3a%2f%2fr3---sn-tv0cgv5qc5oq-nu8e.googlevideo.com%2fvideoplayback%3fsver%3d3%26id%3d852e427cff27fe3b%26dur%3d55.681 ... ty=small
i don't know missing.
here sample project.
Comments
Post a Comment