Search code examples
iosiphoneswiftmpmovieplayercontroller

Playing a movie with MPMoviePlayerController and Swift


I'm trying to MPMoviePlayerController as subview on a view, but i'm trying to do that with Swift, this is what I did so far:

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    let moviePath = NSBundle.mainBundle().pathForResource("splash", ofType: "mp4")
    let movieURL = NSURL.fileURLWithPath(moviePath!)

    let moviePlayer = MPMoviePlayerController(contentURL: movieURL)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "moviePlayerDidFinishPlaying:" , name: MPMoviePlayerPlaybackDidFinishNotification, object: moviePlayer)

    moviePlayer.controlStyle = .None
    moviePlayer.scalingMode = .Fill
    self.view.addSubview(moviePlayer.view)
    moviePlayer.setFullscreen(true, animated: true)
    moviePlayer.play()
}

func moviePlayerDidFinishPlaying(notification: NSNotification) {

}

It's not working, what am I doing wrong here?


Solution

  • You maybe forget add your resources as "Bundle Resources" Follow below step to add:

    1. In the Project Navigator select your project root
    2. Select "Build Phases" tab
    3. In "Bundle Resources" section to click "+" to add your resources

    After that I think you can get your code working fine.