完成播放Swift后关闭视频

时间:2015-05-04 07:42:50

标签: ios swift mpmovieplayercontroller

我正在使用MPMoviePlayerController播放视频。我想在完成播放后关闭视频。这是我的代码:

import UIKit
import MediaPlayer

class programViewController: UIViewController {

    var moviePlayer : MPMoviePlayerController?

    override func viewDidLoad() {
        super.viewDidLoad()
    // Do any additional setup after loading the view.
        playVideo("video", type: "mov")
    }

    func playVideo(navn:String, type:String) {
    let path = NSBundle.mainBundle().pathForResource(navn, ofType: type)
        let url = NSURL.fileURLWithPath(path!)
        moviePlayer = MPMoviePlayerController(contentURL: url)
        if let player = moviePlayer {
            player.view.frame = self.view.bounds
            player.prepareToPlay()
            player.scalingMode = .AspectFit
            player.shouldAutoplay = true
            player.fullscreen = false
            self.view.addSubview(player.view)
        }
    }
}

4 个答案:

答案 0 :(得分:2)

你应该添加playVideo功能

                NSNotificationCenter.defaultCenter().addObserver(self, selector: "movieFinishedCallback:", name: MPMoviePlayerPlaybackDidFinishNotification, object: player)

并在你的课堂上:

    func movieFinishedCallback(notif:NSNotification) {
    // Obtain the reason why the movie playback finished
    var userInfo:Dictionary<String,Int!>  = notif.userInfo as! Dictionary<String,Int!>

    let finishReason : Int = userInfo[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]!

    // Dismiss the view controller ONLY when the reason is not "playback ended"
    if ( finishReason == MPMovieFinishReason.PlaybackEnded.rawValue)
    {
        let moviePlayer:MPMoviePlayerController = notif.object as! MPMoviePlayerController

        // Remove this class from the observers
        NSNotificationCenter.defaultCenter().removeObserver(self, name: MPMoviePlayerPlaybackDidFinishNotification, object: self.moviePlayerController!.moviePlayer)

        // Dismiss the view controller
        moviePlayer.view.removeFromSuperview()
    }
}

答案 1 :(得分:1)

@ roman-barzyczak的更新版本答案:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(movieFinishedCallback(_:)), name: MPMoviePlayerPlaybackDidFinishNotification, object: moviePlayer)

然后:

func movieFinishedCallback(notification: NSNotification) {

    if let userInfo = notification.userInfo as? [String : NSNumber] {
        let reason = userInfo[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]
        let finishReason = MPMovieFinishReason(rawValue: reason!.integerValue)

        if (finishReason != MPMovieFinishReason.PlaybackEnded),
            let moviePlayer = notification.object as? MPMoviePlayerController {

            NSNotificationCenter.defaultCenter().removeObserver(self, name: MPMoviePlayerPlaybackDidFinishNotification, object: moviePlayer)
            moviePlayer.view.removeFromSuperview()
        }
    }
}

答案 2 :(得分:0)

您需要收听MPMoviePlayerPlaybackDidFinishNotification通知。

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("playerPlaybackDidFinish"), name: MPMoviePlayerPlaybackDidFinishNotification, object: nil)

// Present
self.presentViewController(moviePlayer, animated: ture, completion: nil)

// Dismiss
moviewPlayer.dismissViewControllerAnimated(true, completion: nil)

答案 3 :(得分:0)

可以从子视图中删除视图,并且因为最后制作的子视图是电影播放器​​视图,所以您只需使用以下代码即可将其删除:

self.view.subviews[self.view.subviews.count-1].removeFromSuperview()