Swift 2 - fileURLWithPath返回NIL

时间:2015-12-04 04:30:05

标签: swift2 mpmovieplayercontroller mp4

我正在尝试使用MPMoviePlayerController加载视频。我已将该文件添加到我的项目中,并添加到Build Phase" 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()


    }

1 个答案:

答案 0 :(得分:1)

你只需要改变:

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
        }

TO:

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
        }