Search code examples
iosswiftmpmovieplayercontrollermpmovieplayer

After Dismissing MPMoviePlayerViewController a Blank Movie Player Appears


It looks like it is launching a movieplayer before the mpmovieplayerviewcontroller launches its movieplayer.

The blank movie player that appears after cannot be dismissed by clicking the done button.

Here is the code:

class VideoViewController: UIViewController {

    var movieViewController:MPMoviePlayerViewController?

    override func viewDidLoad() {
        let path = NSBundle.mainBundle().pathForResource("Open Range", ofType: "mp4")
        let url = NSURL.fileURLWithPath(path!);
        movieViewController = MPMoviePlayerViewController(contentURL: url)
        NSNotificationCenter.defaultCenter().addObserver(self,
            selector:"doneButtonClick:",
            name:MPMoviePlayerPlaybackDidFinishNotification,
            object:nil)
        NSNotificationCenter.defaultCenter().addObserver(self,
            selector:"doneButtonClick:",
            name:MPMoviePlayerDidExitFullscreenNotification,
            object:nil)
    }
    override func viewDidAppear(animated: Bool) {
        self.presentMoviePlayerViewControllerAnimated(movieViewController)
    }
    @objc func doneButtonClick(notification: NSNotification){
        self.movieViewController?.moviePlayer.stop()
        self.dismissMoviePlayerViewControllerAnimated()
    }
}

Solution

  • Solved. The problem was that I was segueing to a separate view controller that had the MPMoviePlayerViewController in it. When I moved the MPMoviePlayerViewController to the view controller from which I wanted to launch the movie, it returns to the desired view after calling self.dismissMoviePlayerViewControllerAnimated()