fileURLWithPath对AVPlayerController来说是nil

时间:2015-10-06 09:38:16

标签: ios swift2 avplayerviewcontroller

我尝试使用AVPlayer播放视频,但在提供pathForResource时遇到错误。尝试了包装可选值但不成功的所有可能方法。任何人都可以帮助我。

    let userdefault = NSUserDefaults.standardUserDefaults()
    let mediaPath = userdefault.URLForKey("VideoPath")
    print(mediaPath)

    let movieString = mediaPath!.path
    let pathExtension = mediaPath!.pathExtension



    let newPath = movieString!.stringByReplacingOccurrencesOfString(".\(pathExtension)", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
    print(newPath)
    let path = NSBundle.mainBundle().pathForResource(newPath, ofType: ".\(pathExtension)")

    print(path)

    if let pathString = path {
        let url = NSURL(fileURLWithPath: pathString)
        myplayer = AVPlayer(URL: url)
        myplayer.play()
    }
    else {
        print("Oooopppsss, something wrong when playing video")
    }

0 个答案:

没有答案