1

I am trying to load a video using MPMoviePlayerController. I have added the file in my project and also added to the Build Phase "Copy Bundle Resource".

When I call the function to open the video I can see that the path its printed out however, the app crashes on 'fileURLWithPath`.

What am I doing wrong? Are .mp4 flies playable?

 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()


    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
SNos
  • 3,430
  • 5
  • 42
  • 92

1 Answers1

0

You just need to change:

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
        }
Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
  • 1
    Thank you but I get error: Cannot convert NSURL? to string – SNos Dec 04 '15 at 11:09
  • 2
    prints out: file:///var/mobile/Containers/Bundle/Application/5B983567-AD2B-417A-B384-4218562E3796/myApp.app/firstVideo.mp4 but returns fatal error again – SNos Dec 04 '15 at 11:14