私は自分のアプリケーションで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);
}
あなたは[ 'MPMoviePlayerPlaybackDidFinishReasonUserInfoKey'が廃止されました]ということを知っています(https://developer.apple.com/documentation/mediaplayer/mpmovieplayerplaybackdidfinishreasonuserinfokey) ? –