播放完毕后关闭视频 Swift
Dismiss video when it is finished playing Swift
我正在使用 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)
}
}
}
您需要监听 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)
可以只从子视图中删除视图,因为最后创建的子视图是电影播放器视图,您可以使用以下代码删除它:
self.view.subviews[self.view.subviews.count-1].removeFromSuperview()
你应该添加播放视频功能
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()
}
}
@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()
}
}
}
我正在使用 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)
}
}
}
您需要监听 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)
可以只从子视图中删除视图,因为最后创建的子视图是电影播放器视图,您可以使用以下代码删除它:
self.view.subviews[self.view.subviews.count-1].removeFromSuperview()
你应该添加播放视频功能
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()
}
}
@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()
}
}
}