SWIFT:MediaPlayer - 完成按钮没有做任何事情

时间:2015-10-05 02:49:07

标签: ios swift video media-player mplayer

所以我有以下代码工作得很好!对于想要在SWIFT中播放视频的人问题是,在视频完成后,它仍然在视频中。即使按完成也没有任何反应......

如何将视频关闭回其中一个现有的视图控制器?

class ViewController: UIViewController {

var moviePlayer : MPMoviePlayerController?
override func viewDidLoad() {
    super.viewDidLoad()
    playVideo()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func playVideo() {

    //Get the Video Path
    //You need to put this in Project->Target->copy bundle resource for this to work
    let videoPath = NSBundle.mainBundle().pathForResource("Quizzes", ofType:"mov")

    //Make a URL from your path
    let url = NSURL.fileURLWithPath(videoPath!)

    //Initalize the movie player
    moviePlayer = MPMoviePlayerController(contentURL: url)

    if let player = moviePlayer {

        //Make the player scale the entire view
        player.view.frame = self.view.bounds
        player.scalingMode = .AspectFill

        //Add it as a subView to your currentView
        self.view.addSubview(player.view)

        //Play the video
        player.prepareToPlay()

    }
    else {
        print("Movie player couldn't be initialized")
    }
}

1 个答案:

答案 0 :(得分:0)

您已将其添加为子视图

  

self.view.addSubview(player.view)

您还需要删除该代码的位置吗? 完成播放后请使用removeFromSuperview。