Ios 正在获取MPMoviePlayerPlayback完成MPMoviePlayerController的用户密钥nil
我已经在我的应用程序中使用了MPMoviePlayerController。我正在尝试完成MPMoviePlayerController的按钮事件。但我在[MPMoviePlayerPlaybackDidFinishRasonUserInFokey]中得到的值为零,因此应用程序正在崩溃。这是我的代码:Ios 正在获取MPMoviePlayerPlayback完成MPMoviePlayerController的用户密钥nil,ios,swift3,Ios,Swift3,我已经在我的应用程序中使用了MPMoviePlayerController。我正在尝试完成MPMoviePlayerController的按钮事件。但我在[MPMoviePlayerPlaybackDidFinishRasonUserInFokey]中得到的值为零,因此应用程序正在崩溃。这是我的代码: // Define notification in class let notificationName = Notification.Name("moviePlayerDoneButtonCli
// Define notification in class
let notificationName = Notification.Name("moviePlayerDoneButtonClicked")
//---register and posted notification in viewDidLoad()
// Register to receive notification
NotificationCenter.default.addObserver(self, selector: #selector(PlayVideo.moviePlayerDoneButtonClicked), name: self.notificationName, object: nil)
// Post notification
NotificationCenter.default.post(name: self.notificationName, object: nil)
//invoking player
playVideo()
//Event for moviePlayer Done Button Pressed (method for notification)
func moviePlayerDoneButtonClicked(note: NSNotification) {
print ("delegate called....")
let reason = note.userInfo?[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]
if (MPMovieFinishReason(rawValue: reason as! Int) == MPMovieFinishReason.userExited) {
print ("Done button clicked.")
self.delegate.exitVideoPlayer()
}
}
//—code for
func playVideo() {
//--Adjust height and width of player as per device
activityIndicator.isHidden = false
activityIndicator.startAnimating()
print ("self.attachmentURL : \(self.attachmentURL)")
DispatchQueue.main.async {
let url:URL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!
self.moviePlayer = MPMoviePlayerController(contentURL: url)
self.moviePlayer!.view.frame = CGRect(x: 0, y: 0, width: self.width, height: self.height);
self.videoContainerView.addSubview(self.moviePlayer!.view)
self.moviePlayer!.isFullscreen = false
self.moviePlayer!.controlStyle = MPMovieControlStyle.embedded
self.activityIndicator.stopAnimating()
self.activityIndicator.isHidden = true
}
}
override func viewDidDisappear(_ animated: Bool) {
// Stop listening notification
NotificationCenter.default.removeObserver(self, name: notificationName, object: nil);
}
我现在明白你的问题了。您正在发布一个“
moviePlayerDoneButtonClicked
”通知,其中没有对象和userInfo
字典
你为什么这样做?用户单击“完成”按钮,然后iOS/movie player为您的应用程序创建“完成”通知以进行处理
删除NotificationCenter.default.post(名称:self.notificationName,…
在那里排队
并将代码更改为:
//Event for moviePlayer Done Button Pressed (method for notification)
func moviePlayerDoneButtonClicked(notification: NSNotification) {
print ("delegate called....")
if let userInfo = notification.userInfo
{
let finishReason = MPMovieFinishReason(rawValue: (notification.userInfo[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] as! NSNumber).intValue)
if (finishReason == MPMovieFinishReason.userExited)
{
print ("Done button clicked.")
self.delegate.exitVideoPlayer()
}
} else {
print("I shouldn't be manually posting a moviePlayerDoneButtonClicked notification without a userInfo dictionary")
}
}
你知道的,还有MPMoviePlayerController,对吗?我的FinishReson为零。应用程序在这条线上崩溃,让FinishReson=MPMovieFinishReson(rawValue:(notification.userInfo![MPMoviePlayerPlayback diFinishResunserinFokey]as!NSNumber).intValue)我在那里输入了一个错误-
通知
应该是注释
。它仍然崩溃吗?是的,我更正了,它仍然崩溃。我为注册和发布通知编写的代码正确吗?我看到了注册和发布通知的行,但它们使用的是哪个功能?(例如查看加载
)我已经在viewDidLoad中编写了register和post通知代码,然后调用playVideo()方法。