Swift - Can't Dismiss MPMoviePlayerViewController

前端 未结 1 1191
心在旅途
心在旅途 2021-01-17 20:20

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. Th

相关标签:
1条回答
  • 2021-01-17 20:52

    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()
        }
    }
    
    0 讨论(0)
提交回复
热议问题