قالب وردپرس درنا توس
Home / IOS Development / Is there a better way to look for a key value change?

Is there a better way to look for a key value change?



I wonder if the following really is the best way to know when a video is ready to be played: At the moment, what am I doing if currentMedia.playerQueue? .Status.rawValue == 1 { I play, else (the video hasn't loaded yet) I want to show the loading icon and then do the following to see when the video is ready to play:

I create an AVPlayer using the same video url and then when it is ready I will call the playCurentMedia () method to play at.

  currentMedia.avPlayer = AVPlayer (url: currentMedia.videoURL!)

currentMedia.avPlayer! .addObserver (self, forKeyPath: "status", options: [.new, .initial] context: & P2SheetViewController.playerStatusContext)

Does this stragtegy have no fault? Or is it a good solution?

How can I observe a change so that I can view the video when it's ready?

Currently I am only able to set up an AVPlayer along the queue so I can observe for change …

  override func observerValue (forKeyPath keyPath: String ?, Of object: Any?, Change: [NSKeyValueChangeKey : Any]?, Context: UnsafeMutableRawPointer?) {
print ("observe KVO")
// Only handle observations for playerItemContext
guard context == & P2SheetViewController.playerStatusContext other {
super.observeValue (forKeyPath: keyPath, of: object, change: change, context: context)
return
}

if keyPath == #keyPath (AVPlayer.status) {
la status: AVPlayer.Status
if let statusNumber = change? [.newKey] as? NSNumber {
status = AVPlayer.Status (rawValue: statusNumber.intValue)!
} other {
status =. unknown
}

// Change status value
change status {
case .readyToPlay:
print ("READY TO PLAY")
GlobalSharedData.shared.videoAllSetToGoMedia1
= true if GlobalSharedData.shared.p2Media1VideoWasNotReadyWhenPressionView { baseVC.playVideoControlForP2 () } break // The player is ready to play. case. failed: print ("unknown") break // Player's element failed. See wrong. case. unknown: print ("unknown") break // The player object is not yet ready. } } }

Source link