2015-05-04 103 views
0

我正在使用MPMoviePlayerController播放視頻。我想在視頻播放結束後關閉視頻。這裏是我的代碼:當播放完成後關閉視頻Swift

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

回答

0

很可能只是刪除子視圖的視圖,因爲最後做子視圖是電影播放器​​視圖中你可以通過使用此代碼刪除:

self.view.subviews[self.view.subviews.count-1].removeFromSuperview() 
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) 
+0

我寫了 「playerPlaybackDidFinish」 FUNC裏面有什麼? – vladasha

+0

在我的理解中,你應該像這樣呈現moviePlayer:self.presentViewController(mv,animated:ture,completion:nil) – Ramis

+0

然後解僱像這樣:moviePlayer.dismissViewControllerAnimated(true,completion:nil) – Ramis

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

謝謝,隊友!好的。 – Felipe

+0

看起來你只是在PlaybackEnded上解僱 – benaneesh

1

更新的版本@羅馬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() 
     } 
    } 
}