virusss8
virusss8

Reputation: 235

AVPlayer removeTimeObserver crashes app (swift 3)

I have global variable time observer set as:

self.timeObserver = self.avPlayer?.addPeriodicTimeObserver(forInterval: CMTime(seconds: 0.5, preferredTimescale: CMTimeScale(NSEC_PER_SEC)), queue: DispatchQueue.main) { [weak self] time in
      guard let weakSelf = self else {
          return
      }
      // updating label with time
}

when view is getting closed I call:

if self.avPlayer != nil {
   self.avPlayer!.removeTimeObserver(self.timeObserver)
}

but that crashes the app saying:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'You did not supply a reference to an object returned by either -addPeriodicTimeObserverForInterval:queue:usingBlock: or -addBoundaryTimeObserverForTimes:queue:usingBlock:'

Please tell me what am I doing wrong? Thank You

Upvotes: 5

Views: 4868

Answers (1)

edwinzhang
edwinzhang

Reputation: 146

You have to unwrap self.timeObserver.

removeTimeObserver() takes in Any object, but you are passing in Any?, which causes the crash.

Upvotes: 12

Related Questions