【问题标题】:Getting MPMoviePlayerPlaybackDidFinishReasonUserInfoKey of MPMoviePlayerController nil获取 MPMoviePlayerController 的 MPMoviePlayerPlaybackDidFinishReasonUserInfoKey nil
【发布时间】:2017-09-18 06:09:01
【问题描述】:

我在我的应用程序中使用了 MPMoviePlayerController。我正在尝试获取 MPMoviePlayerController 的完成按钮事件。但是我在 [MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] 中得到了 nil 值,因此应用程序崩溃了。这是我的代码:

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

【问题讨论】:

标签: ios swift3


【解决方案1】:

我现在看到了你的问题。您正在发布一个“moviePlayerDoneButtonClicked”通知,其中没有对象,也没有userInfo 字典。

你为什么要这样做?用户是单击“完成”按钮的人,然后 iOS / 电影播放器​​创建“完成”通知供您的应用处理。

删除那里的NotificationCenter.default.post(name: 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")
    }
}

【讨论】:

  • 我得到了finishReason nil。应用程序在这条线上崩溃 let finishReason = MPMovieFinishReason(rawValue: (notification.userInfo![MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] as!NSNumber).intValue)
  • 我有一个错字 - notification 应该是 note。它还在崩溃吗?
  • ..是的,我更正了,它仍然崩溃。我写的用于注册和发布通知的代码对吗?
  • 我看到了您注册和发布通知的行,但它们位于哪个函数中? (例如viewDidLoad??)
  • 我已经在viewDidLoad中写了注册并发布了一个通知代码,然后调用了playVideo()方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多