Swift 2 - fileURLWithPath returns 无

Swift 2 - fileURLWithPath returns NIL

我正在尝试使用 MPMoviePlayerController 加载视频。我已将文件添加到我的项目中,并且还添加到构建阶段 "Copy Bundle Resource".

当我调用打开视频的函数时,我可以看到打印出的路径,但是,应用程序在“fileURLWithPath”上崩溃。

我做错了什么? .mp4 苍蝇可以玩吗?

 func firstView(){

        if let filePath  = NSBundle.mainBundle().pathForResource("firstVideo", ofType: "mp4") {

            print(filePath)
//PRINTS: /var/mobile/Containers/Bundle/Application/B93BB049-DA87-4D89-AEBE-A5C92C01726E/MyApp.app/firstVideo.mp4

            firstMoviePlayer!.contentURL = NSURL.fileURLWithPath(filePath) // fatal error
        }


        firstMoviePlayer!.repeatMode = .None
        firstMoviePlayer!.controlStyle = MPMovieControlStyle.Embedded

        firstMoviePlayer!.view.frame = FirstTimeVideoView.frame
        FirstTimeVideoView.addSubview(firstMoviePlayer!.view)

        firstMoviePlayer!.play()


    }

你只需要改变:

if let filePath  = NSBundle.mainBundle().pathForResource("firstVideo", ofType: "mp4") {

            print(filePath)
//PRINTS: /var/mobile/Containers/Bundle/Application/B93BB049-DA87-4D89-AEBE-A5C92C01726E/MyApp.app/firstVideo.mp4

            firstMoviePlayer!.contentURL = NSURL.fileURLWithPath(filePath) // fatal error
        }

收件人:

if let filePath  = NSBundle.mainBundle().URLForResource("firstVideo", ofType: "mp4") {

            print(filePath)
//PRINTS: /var/mobile/Containers/Bundle/Application/B93BB049-DA87-4D89-AEBE-A5C92C01726E/MyApp.app/firstVideo.mp4

            firstMoviePlayer!.contentURL = NSURL.fileURLWithPath(filePath) // fatal error
        }