I need to log analytics for user forward/rewind events on AVPlayer. Is there any way to know user tapped forward/rewind button on AVPlayer and the user manually seek froward/rewind in Swift
-
Does this answer your question? [AVPlayer - Fast Backward / Forward Stream](https://stackoverflow.com/questions/15094948/avplayer-fast-backward-forward-stream) – El Tomato Jul 04 '20 at 02:40
-
No, I just need a callback or notification when user forward/rewind – Ragul Jul 04 '20 at 03:09
3 Answers
Try using NSNotification.Name.AVPlayerItemTimeJumped
notification.
Description: A notification that’s posted when the item’s current time has changed discontinuously.
You should be able to know when there is forwards and rewinds what you need with this.
https://developer.apple.com/documentation/foundation/nsnotification/name/1390911-avplayeritemtimejumped
For detecting when users seek you should be able to use an observer and get the job done.
Maybe addPeriodicTimeObserver(forInterval:queue:using:) is helpful to you as well.

- 374
- 1
- 13
https://developer.apple.com/documentation/mediaplayer/mpremotecommandcenter
MPRemoteCommandCenter.shared().seekForwardCommand
MPRemoteCommandCenter.shared().nextTrackCommand
MPMediaPlayer
also has MPChangePlaybackPositionCommand
You will need to call: https://developer.apple.com/documentation/uikit/uiapplication/1623126-beginreceivingremotecontrolevent

- 22,723
- 11
- 93
- 186
These playback controls are system provided controls and I see there are no observers or delegates provided to identify when these events are triggered by user. However, you can do below to get to know user seeks using progress bar. When user seeks you will get below notification.
NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemTimeJumped, object: playerItem, queue: OperationQueue.main) { (notification) in
print("AVPlayerItemTimeJumped : \(notification) \(playerItem.currentTime())")
}
But I think it is not that reliable because when I seek from one point to another point from the progress bar, I get below 4 notifications as shown in the log. But the seek even can be identified by adding some logic to determined the event.
AVPlayerItemTimeJumped : name = AVPlayerItemTimeJumpedNotification, object = Optional(<AVPlayerItem: 0x600000e102d0, asset = <AVURLAsset: 0x600000c20a00, URL = ), userInfo = nil CMTime(value: 2753401, timescale: 90000, flags: __C.CMTimeFlags(rawValue: 3), epoch: 0)
AVPlayerItemTimeJumped : name = AVPlayerItemTimeJumpedNotification, object = Optional(<AVPlayerItem: 0x600000e102d0, asset = <AVURLAsset: 0x600000c20a00, URL = ), userInfo = nil CMTime(value: 8460000, timescale: 90000, flags: __C.CMTimeFlags(rawValue: 1), epoch: 0)
AVPlayerItemTimeJumped : name = AVPlayerItemTimeJumpedNotification, object = Optional(<AVPlayerItem: 0x600000e102d0, asset = <AVURLAsset: 0x600000c20a00, URL = ), userInfo = nil CMTime(value: 84600009, timescale: 900000, flags: __C.CMTimeFlags(rawValue: 1), epoch: 0)
AVPlayerItemTimeJumped : name = AVPlayerItemTimeJumpedNotification, object = Optional(<AVPlayerItem: 0x600000e102d0, asset = <AVURLAsset: 0x600000c20a00, URL = ), userInfo = nil CMTime(value: 84600009, timescale: 900000, flags: __C.CMTimeFlags(rawValue: 1), epoch: 0)
If you want more flexibility over the Playback controls of the AVPlayer then I think better to disable the system provided playback controls and create custom Playback controls and you get better hold on them. To do that you can disable as below :
let controller = AVPlayerViewController()
controller.player = player
controller.showsPlaybackControls = false
And then you can add your custom playback control buttons as demonstrated in below links: https://medium.com/flawless-app-stories/avplayer-swiftui-part-2-player-controls-c28b721e7e27
Or you can also added some identifier over the AVPlayerViewController as explained in below link: Custom control for playback speed for AVPlayerViewController
Hope this information helps.

- 1,136
- 9
- 16