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];
[self replaceView: thisCurrentView withView: thisReplacementView];
NSString *filepath = [[NSBundle mainBundle] pathForResource:theString ofType:@"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
controlledPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem];
AVPlayerLayer *animatedLayer = [AVPlayerLayer playerLayerWithPlayer:controlledPlayer];
[animatedLayer setFrame:CGRectMake(0, 0, 1024, 1024)];
[thisReplacementView.layer addSublayer: animatedLayer];
[controlledPlayer play];
[self displayNoContent];
pageContent = theString;
playingStatus = YES;
}
- (void) itemDidFinishPlaying {
[self displayContent: pageContent]
}
source
share