Search code examples
iosswiftmpmovieplayercontroller

MPMoviePlayerViewController black screen after returning from background


I have a simple controller that plays a video when opened. This works, but when I put the app into the backround and return I get a blank or black screen. If I then navigate to another view and then return, the view is no longer black and the video is playing.

How can I have the video play upon returning from the background?

MyViewController:

var myMovieController : MPMoviePlayerViewController?

override func viewDidLoad() {
    super.viewDidLoad()
    self.initMovie()
}

func  initMovie(){
    let moviePath = NSBundle.mainBundle().pathForResource("my-movie", ofType: "mp4")
    let movieURL = NSURL.fileURLWithPath(moviePath!)
    self.myMovieController = MPMoviePlayerViewController(contentURL: movieURL)
    self.myMovieController?.moviePlayer.view.frame = self.view.bounds
    self.myMovieController?.moviePlayer.controlStyle = MPMovieControlStyle.None
    self.myMovieController?.moviePlayer.prepareToPlay()
    self.myMovieController?.moviePlayer.scalingMode = .AspectFill
    self.myMovieController?.moviePlayer.fullscreen = true
    self.myMovieController?.moviePlayer.repeatMode = MPMovieRepeatMode.One
    self.view.insertSubview(myMovieController!.moviePlayer.view, atIndex:0)
    self.myMovieController?.moviePlayer.play()
}

Solution

  • In applicationWillEnterForeground I got a reference to MyViewController and called play on myMovieController. Not sure if this is the correct way to go about it, but it seems to work.

    func applicationWillEnterForeground(application: UIApplication) {
        let mc: MyViewController = (window!.rootViewController as! MyViewController)
        mc.myMovieController?.moviePlayer.prepareToPlay()
        mc.myMovieController?.moviePlayer.play()
    }