2017-09-18 5 views
2

私は自分のアプリケーションでMPMoviePlayerControllerを使用しています。 MPMoviePlayerControllerのDoneボタンイベントを取得しようとしています。しかし、私は[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]で無価値を得ています。そのため、アプリがクラッシュしています。これは私のコードです:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey of MPMoviePlayerControllerの取得なし

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

あなたは[ 'MPMoviePlayerPlaybackDidFinishReasonUserInfoKey'が廃止されました]ということを知っています(https://developer.apple.com/documentation/mediaplayer/mpmovieplayerplaybackdidfinishreasonuserinfokey) ? –

答えて

0

私は今あなたの問題を参照してください。オブジェクトがなく、userInfo辞書がない "moviePlayerDoneButtonClicked"通知を投稿しています。

なぜこのようにしていますか?ユーザーは「完了」ボタンをクリックした後、iOS /ムービープレーヤーはあなたのアプリが処理するための「完了」通知を作成します。

NotificationCenter.default.post(name: self.notificationName, ...を削除してください。

そして、これにコードを変更:MPMoviePlayerControllerの権利とともに、

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

私はfinishReason nilを得ています。アプリケーションはこの行でクラッシュするfinishReason = MPMovieFinishReason(rawValue:(notification.userInfo![MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] as!NSNumber).intValue) –

+0

私はそこに入力ミスがありました。それはまだクラッシュですか? –

+0

..はい、私は修正しました、それはまだクラッシュしています。私が書いたコードは、登録と通知の権利のために書かれていますか? –

関連する問題