使用swift下载YouTube视频的问题

时间:2015-07-10 10:33:40

标签: swift video nsdata fatal-error nsurl

我正在尝试在我的应用中下载youtube视频,为此我使用了THIS和该答案中包含的库,但我的应用程序崩溃并出现错误:

  

致命错误:在解包可选值时意外发现nil

这是我的快捷代码:

let attr = NSAttributedString(attributedString: textView.attributedText)

我的项目在这一行崩溃了:

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) successfully saved")
                    })
                } else {
                    println("Failed extracting video URL using block due to error:\(error)")
                }

            })
        }
    }
}

但是我可以在控制台中获得let data = NSData(contentsOfURL: videoURL)!

  

的http%3A%2F%2Fr3 --- sn-tv0cgv5qc5oq-nu8e.googlevideo.com%2Fvideoplayback%3Fsver%3D3%26id%3D852e427cff27fe3b%26dur%3D55.681   ...... ty = small

我不知道我错过了什么。

Here是我的示例项目。

0 个答案:

没有答案