Skip to content

Commit

Permalink
Merge pull request #21 from oliviermartin08/fix/seek-events-when-paused
Browse files Browse the repository at this point in the history
Handle correctly seek events while paused
  • Loading branch information
miransar authored Jul 24, 2023
2 parents 41b7602 + 2d0c57c commit b9aae80
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 27 deletions.
37 changes: 27 additions & 10 deletions NRAVPlayerTracker/NRAVPlayerTracker/Tracker/NRTrackerAVPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ - (void)unregisterListeners {
}
@catch (id e) {}

@try {
[self.state removeObserver:self forKeyPath:@"isUserSeeking"];
}
@catch (id e) {}

@try {
[self.playerInstance removeTimeObserver:self.timeObserver];
self.timeObserver = nil;
Expand Down Expand Up @@ -176,6 +181,11 @@ - (void)registerListeners {
options:NSKeyValueObservingOptionNew
context:NULL];

[self.state addObserver:self
forKeyPath:@"isUserSeeking"
options:NSKeyValueObservingOptionNew
context:NULL];

self.timeObserver =
[self.playerInstance addPeriodicTimeObserverForInterval:CMTimeMake(1, 2) queue:NULL usingBlock:^(CMTime time) {

Expand Down Expand Up @@ -210,19 +220,26 @@ - (void)observeValueForKeyPath:(NSString *)keyPath
context:(void *)context {

AV_LOG(@"(AVPlayerTracker) Observed keyPath = %@ , object = %@ , change = %@ , context = %@", keyPath, object, change, context);

if ([keyPath isEqualToString:@"currentItem.playbackBufferEmpty"]) {
if (!self.state.isBuffering && self.state.isPaused && self.playerInstance.rate == 0.0) {
[self sendSeekStart];
}
// The state isSeekingDuringPlayback can be set manually using the setIsSeekingDuringPlayback function
// Otherwise, KVO observers are not being notified that a seek happened during playback (rate == 1.0)
else if (!self.state.isBuffering && self.state.isSeekingDuringPlayback && !self.state.isPaused && self.playerInstance.rate == 1.0) {
// User seek event sent by the integrators
if ([keyPath isEqualToString:@"isUserSeeking"]) {
if (self.state.isUserSeeking) {
[self sendSeekStart];
}
}
else if ([keyPath isEqualToString:@"currentItem.playbackBufferEmpty"] && self.state.isSeeking && self.state.isPaused) {
[self sendBufferStart];
}
else if ([keyPath isEqualToString:@"currentItem.playbackBufferFull"] && self.state.isSeeking && self.state.isPaused) {
[self sendBufferEnd];
[self sendSeekEnd];
}
else if ([keyPath isEqualToString:@"currentItem.playbackLikelyToKeepUp"]) {
[self sendRequest];

if (self.state.isSeeking && self.state.isPaused && self.playerInstance.currentItem.playbackLikelyToKeepUp) {
[self sendBufferEnd];
[self sendSeekEnd];
}
}
else if ([keyPath isEqualToString:@"status"]) {
if (self.playerInstance.status == AVPlayerItemStatusReadyToPlay) {
Expand Down Expand Up @@ -255,6 +272,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath
}
else {
[self sendBufferEnd];
[self sendSeekEnd];
}
} else {
// Fallback on earlier versions
Expand Down Expand Up @@ -364,7 +382,7 @@ - (NSString *)getTrackerName {
}

- (NSString *)getTrackerVersion {
return @"1.0.0";
return @"2.0.0";
}

- (NSString *)getPlayerVersion {
Expand Down Expand Up @@ -459,7 +477,6 @@ - (void)sendEnd {

- (void)sendResume {
//Make sure we close the previous blocks
[self sendSeekEnd];
// Send RESUME
[super sendResume];

Expand Down
2 changes: 1 addition & 1 deletion NRIMATracker/NRIMATracker/Tracker/NRTrackerIMA.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ - (NSString *)getTrackerName {
}

- (NSString *)getTrackerVersion {
return @"1.0.0";
return @"2.0.0";
}

- (NSNumber *)getPlayhead {
Expand Down
10 changes: 5 additions & 5 deletions NewRelicVideoCore/NewRelicVideoCore/Model/NRTrackerState.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ NS_ASSUME_NONNULL_BEGIN
- (void)reset;

/**
Set the state isSeekingDuringPlayback.
This function has to be called at least when seeking occurs during playback because the KVO observer methods will not catch this type of seek.
Set the state isUserSeeking to true.
This function has to be called whenever a seek event occurs during playback.
*/
- (void)setIsSeekingDuringPlayback;
- (void)startSeekingEvent;

/**
Return state isPlayerReady.
Expand Down Expand Up @@ -68,11 +68,11 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)isSeeking;

/**
Return state isSeekingDuringPlayback.
Return state isUserSeeking.
@return state..
*/
- (BOOL)isSeekingDuringPlayback;
- (BOOL)isUserSeeking;

/**
Return state isBuffering.
Expand Down
16 changes: 6 additions & 10 deletions NewRelicVideoCore/NewRelicVideoCore/Model/NRTrackerState.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ @interface NRTrackerState ()
@property (nonatomic) BOOL isPlaying;
@property (nonatomic) BOOL isPaused;
@property (nonatomic) BOOL isSeeking;
@property (nonatomic) BOOL isSeekingDuringPlayback;
@property (nonatomic) BOOL isUserSeeking;
@property (nonatomic) BOOL isBuffering;
@property (nonatomic) BOOL isAd;
@property (nonatomic) BOOL isAdBreak;
Expand All @@ -38,19 +38,14 @@ - (void)reset {
self.isPlaying = NO;
self.isPaused = NO;
self.isSeeking = NO;
self.isSeekingDuringPlayback = NO;
self.isUserSeeking = NO;
self.isBuffering = NO;
self.isAd = NO;
self.isAdBreak = NO;
}

- (void)setIsSeekingDuringPlayback {
if (self.isPlaying) {
self.isSeekingDuringPlayback = true;
}
else {
self.isSeekingDuringPlayback = false;
}
- (void)startSeekingEvent {
self.isUserSeeking = true;
}

- (BOOL)goPlayerReady {
Expand Down Expand Up @@ -91,6 +86,7 @@ - (BOOL)goEnd {
self.isPlaying = NO;
self.isPaused = NO;
self.isSeeking = NO;
self.isUserSeeking = NO;
self.isBuffering = NO;
return true;
}
Expand Down Expand Up @@ -155,7 +151,7 @@ - (BOOL)goSeekStart {
- (BOOL)goSeekEnd {
if (self.isStarted && self.isSeeking) {
self.isSeeking = false;
self.isSeekingDuringPlayback = false;
self.isUserSeeking = false;
self.isPlaying = true;
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion NewRelicVideoCore/NewRelicVideoCore/NRVideoDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#ifndef NRVideoDefs_h
#define NRVideoDefs_h

#define NRVIDEO_CORE_VERSION @"1.0.0"
#define NRVIDEO_CORE_VERSION @"2.0.0"

#define NR_VIDEO_EVENT @"MobileVideo"

Expand Down

0 comments on commit b9aae80

Please sign in to comment.