I try to play a video using a MPMoviePlayerController. The setup is: I push a new ViewController, then set up the view and the movie player instance in viewDidLoad and then use a NSURLSession.sharedSession().dataTaskWithURL() where I load the REST resource for the movie to give me the URL. In the completion block I set the contentUrl of the movie player instance to this url and say play. However, the movie frame stays black. If I set the contentUrl hardcoded to the url either in viewDidLoad, viewWillAppear, or viewDidAppear, the movie shows just fine. The errorLog and accessLog are both nil. So I assume something is wrong with the asynchronous url loading and assigning of the movie contentUrl.
Setup: Swift, Xcode 6 beta, iOS 8.
Below some code snippets:
class PresentationsViewController {
override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
let presentationViewController = PresentationViewController(presentations[indexPath.row])
navigationController.pushViewController(presentationViewController, animated: true)
}
}
class PresentationViewController {
var presentation: Presentation?
var moviePlayer: MPMoviePlayerController?
convenience init(_ presentation: Presentation) {
self.init()
self.presentation = presentation
}
override func viewDidLoad() {
super.viewDidLoad()
moviePlayer = MPMoviePlayerController()
moviePlayer!.view.frame = CGRect(x: X, y: Y, width: W, height: H)
moviePlayer!.movieSourceType = MPMovieSourceType.Unknown
moviePlayer!.controlStyle = MPMovieControlStyle.Embedded
NSURLSession.sharedSession().dataTaskWithURL(presentation.url) { data, response, error in
// Some JSON parsing etc.
self.moviePlayer!.contentURL = presentation.videoUrl
self.moviePlayer!.prepareToPlay()
self.moviePlayer!.play()
}.resume()
view.addSubview(moviePlayer.view)
}
}