从后台

时间:2015-05-20 19:57:47

标签: ios swift mpmovieplayercontroller

我有一个简单的控制器,可以在打开时播放视频。这是有效的,但当我将应用程序放入背景并返回时,我得到一个空白或黑屏。如果我然后导航到另一个视图然后返回,则视图不再是黑色并且视频正在播放。

如何从后台返回时播放视频?

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

1 个答案:

答案 0 :(得分:0)

在applicationWillEnterForeground中,我获得了对MyViewController的引用,并在myMovieController上调用了play。不确定这是否是正确的方法,但似乎有效。

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