Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Can't Dismiss MPMoviePlayerViewController

I have a video which opens in an MPMoviePlayerController in my app. It all works great except for the Done button which should close the player. The first time that the video is played the Done button works great. But if you pause it when you are watching it, then hit Done the second time you try to play the video, the Done button doesn't work. I have made a screen recording here to make it a bit simpler to understand: http://1drv.ms/1Jcdodc

Can anyone help?

This is my code that I am using:

import UIKit
import MediaPlayer

class MainContent: UIViewController {

//Movie Player variables
    var movieViewController : MPMoviePlayerViewController?
    var movieplayer : MPMoviePlayerController!


override func viewDidLoad() {

        super.viewDidLoad()

        //Video Player setup
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "doneButtonClick:", name: MPMoviePlayerPlaybackDidFinishNotification, object: nil)

        var url = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!
        movieViewController = MPMoviePlayerViewController(contentURL: url)

}



func doneButtonClick(sender:NSNotification?){
    let value = UIInterfaceOrientation.Portrait.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")
    }

        //when watch button is pressed, present the movie player
    @IBAction func watchPressed(sender: AnyObject)
    {self.presentMoviePlayerViewControllerAnimated(movieViewController)}

 }
like image 521
dwinnbrown Avatar asked Jun 19 '15 13:06

dwinnbrown


1 Answers

To fix this issue, I added the myMoviePlayerViewController.moviePlayer.stop() to the 'doneButtonClick' function. I then added the myMoviePlayerViewController.moviePlayer.play() again to the

@IBAction func watchPressed(sender: AnyObject)
    {self.presentMoviePlayerViewControllerAnimated(movieViewController)}

 }

So all in all a simple fix! Code is below:

import UIKit
import MediaPlayer

class MainContent: UIViewController {

//Movie Player variables
    var movieViewController : MPMoviePlayerViewController?
    var movieplayer : MPMoviePlayerController!


override func viewDidLoad() {

        super.viewDidLoad()

        //Video Player setup
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "doneButtonClick:", name: MPMoviePlayerPlaybackDidFinishNotification, object: nil)

        var url = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!
        movieViewController = MPMoviePlayerViewController(contentURL: url)

}

 func doneButtonClick(sender:NSNotification?){
    let value = UIInterfaceOrientation.Portrait.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")
    movieViewController?.moviePlayer.stop()
  }

    @IBAction func watchPressed(sender: AnyObject){
        self.presentMoviePlayerViewControllerAnimated(movieViewController)
        movieViewController?.moviePlayer.play()
    }
}
like image 64
dwinnbrown Avatar answered Oct 30 '22 09:10

dwinnbrown