Swift - Can't Dismiss MPMoviePlayerViewController

孤人 提交于 2019-12-04 03:03:45

问题


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

 }

回答1:


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


来源:https://stackoverflow.com/questions/30939570/swift-cant-dismiss-mpmovieplayerviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!