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