Observer in NSNotification (itemDidFinishPlaying) RANDOMLY DOUBLE CALL

I show the text as soon as the video is played. For this, I use the notification method. The only problem is that Observer is called twice every time. It runs "itemDidFinishPlaying" twice (and therefore a method with the same name). I can not predict when. I do not know why. It looks random (I know it sounds weird), as if it works great, say 15 times in a row, and the next time this behavior is due to blue. I am rebuilding and launching the application, and this time it works fine 19 times in a row before invoking Observer twice, etc ... Unpredictable. I tried every scenario to predict the error, to fix it. So far this has been impossible. Therefore, I have 2 questions.

1) Why does this happen "by accident"?

2) How to fix this double call problem?

Also, these two following conversations did not help:

Why is the Observer in NSNotification called twice ....?

How to stop Observer in NSNotification to call twice?

Please find my code below:

- (void) playAnimation: (NSString *) theString {

UIView *thisCurrentView = self.currentView;
UIView *thisReplacementView = [[UIView alloc] init];

//[avPlayer pause];
[self replaceView: thisCurrentView withView: thisReplacementView];

NSString *filepath = [[NSBundle mainBundle] pathForResource:theString ofType:@"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];


 // First create an AVPlayerItem
 AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:fileURL];

 // Subscribe to the AVPlayerItem DidPlayToEndTime notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];

 // Pass the AVPlayerItem to a new player
 controlledPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem];


AVPlayerLayer *animatedLayer = [AVPlayerLayer playerLayerWithPlayer:controlledPlayer];


[animatedLayer setFrame:CGRectMake(0, 0, 1024, 1024)];
[thisReplacementView.layer addSublayer: animatedLayer];


// Begin playback
[controlledPlayer play];

// Clear some content
[self displayNoContent];

pageContent = theString;


playingStatus = YES;

}

- (void) itemDidFinishPlaying {

[self displayContent: pageContent];

}

+5
source share
2 answers

Try it,

-(void)itemDidFinishPlaying {

      [self displayContent: pageContent];
      [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];

}

This might work for you.

EDIT1:

Remove the notification observer each time before installing a new one. Try the script below. It will ensure that the previous observer is deleted (even if it does not exist) and adds a new one.

// Subscribe to the AVPlayerItem DidPlayToEndTime notification.

[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
+7
source

This only happens to me during AirPlay. I use the following workaround to ignore duplicate notification.

if ([notificaiton.object isEqual:self.player.currentItem] && (self.player.rate > 0))
{
    [self.player pause];

    //Do your stuff here.
}

NSUserDefault , .

+1

All Articles