     }
 
     @objc private func playbackDidEnd(notification: NSNotification? = nil) {
-        if let object = notification?.object as? AVPlayerItem, let item = player?.currentItem {
-            if object == item {
-                let duration = item.duration
-                let position = item.currentTime()
-                if fabs(CMTimeGetSeconds(duration) - CMTimeGetSeconds(position)) <= 2.0 {
-                    trigger(.didComplete)
-                    updateState(.idle)
-                }
-            }
+        guard let object = notification?.object as? AVPlayerItem, let item = player?.currentItem, object == item else { return }
+        if fabs(CMTimeGetSeconds(item.duration) - CMTimeGetSeconds(item.currentTime())) <= 2.0 {
+            trigger(.didComplete)
+            updateState(.idle)
         }
     }
 

    }

    private func handleLoadedTimeRangesEvent() {
        guard let timeRange = player?.currentItem?.loadedTimeRanges.first?.timeRangeValue else {
            return
        }

        let info = [
            "start_position": CMTimeGetSeconds(timeRange.start),
            "end_position": CMTimeGetSeconds(CMTimeAdd(timeRange.start, timeRange.duration)),
            "duration": CMTimeGetSeconds(timeRange.duration),
            ]

        trigger(.didUpdateBuffer, userInfo: info)
    }


    }

    private func removeObservers() {
        guard let player = player, player.observationInfo != nil else { return }
        player.removeObserver(self, forKeyPath: "currentItem.status")
        player.removeObserver(self, forKeyPath: "currentItem.loadedTimeRanges")
        player.removeObserver(self, forKeyPath: "currentItem.seekableTimeRanges")
        player.removeObserver(self, forKeyPath: "currentItem.playbackLikelyToKeepUp")
        player.removeObserver(self, forKeyPath: "currentItem.playbackBufferEmpty")
        player.removeObserver(self, forKeyPath: "externalPlaybackActive")
        player.removeObserver(self, forKeyPath: "rate")
        if let timeObserver = timeObserver {
            player.removeTimeObserver(timeObserver)
        }
        view.removeObserver(self, forKeyPath: "bounds")
    }
