MPMovieController 不会在“完成”按钮上关闭

MPMovieController won't dismiss on Done button

我的电影文件启动没有问题。完成按钮不会关闭视频内容。不知道为什么?此外,快进和快退按钮只会导致黑屏。我觉得我没有正确使用通知功能?

import Foundation
import UIKit
import MediaPlayer

class VideoViewController: UIViewController {

var moviePlayer:MPMoviePlayerController!

@IBAction func videoLaunch(sender: AnyObject) {
    playVideo()
}
func playVideo() {
let path = NSBundle.mainBundle().pathForResource("MyVideo", ofType:"mp4")
let url = NSURL.fileURLWithPath(path!)
moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = moviePlayer {
player.view.frame = self.view.bounds
moviePlayer?.controlStyle = MPMovieControlStyle.Fullscreen
player.prepareToPlay()
self.view.addSubview(player.view)

}
}


override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: "moviePlayBackDidFinish:",
        name: MPMoviePlayerPlaybackDidFinishNotification,
        object: moviePlayer)


    func moviePlayBackDidFinish(notification: NSNotification){
        self.view.removeFromSuperview()
    }



    }
}

您正在将播放器视图添加为子视图。按下完成按钮后,您应该将其删除(removeFromSuperview)。使用通知监听播放结束:

NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "moviePlayBackDidFinish:",
name: MPMoviePlayerPlaybackDidFinishNotification,
object: moviePlayer)

和 moviePlayBackDidFinish:

func moviePlayBackDidFinish(notification: NSNotification){
  // remove from superview
}

您应该像这样从超级视图中删除 moviePlayer:

   func moviePlayBackDidFinish(notification: NSNotification){
        let moviePlayer:MPMoviePlayerController = notif.object as! MPMoviePlayerController

    moviePlayer.view.removeFromSuperview()
}

因为在你的情况下你删除了 self.view