NSNotification中的观察者(itemDidFinishPlaying)随机调用两次

问题描述 投票:6回答:3

视频播放完毕后,我正在显示文本。我正在使用通知技术来实现这一目标。唯一的问题是Observer每隔一段时间被调用两次。它触发“itemDidFinishPlaying”两次(因此同名的方法)。我无法预测何时。我不知道为什么。它看起来是随机的(我知道这听起来很奇怪)就好像它工作得很好让我们说连续15次,然后下一次这种行为突然发生了。我做了一个重建并运行应用程序,这次它连续运行19次,然后两次调用Observer等等......不可预测。我已经尝试过每个场景来预测bug以便修复它。到目前为止,这是不可能的。所以我有2个问题。

1)为什么会发生并“随机”?

2)如何解决这个双重调用问题?

这两个以下的对话也没有帮助:

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

How to stop the Observer in NSNotification to called twice?

请在下面找到我的代码:

- (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's 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];

}

ios objective-c uitextview avplayer nsnotification
3个回答
8
投票

试试这个,

-(void)itemDidFinishPlaying {

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

}

它可能适合你。

EDIT1

在设置新方案之前,每次都要删除通知观察器。尝试下面的方案。它将确保删除前一个观察者(即使它不存在也没问题)并添加新的观察者。

// Subscribe to the AVPlayerItem's DidPlayToEndTime notification.

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

1
投票

这只发生在AirPlay期间。我正在使用以下解决方法忽略重复通知。

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

    //Do your stuff here.
}

NSUserDefault的答案也有效,但是你必须再次添加观察者,可能会很复杂,具体取决于你的设置。


0
投票

1)验证您只有一个观察者,而不是视图控制器悬挂的观察者没有按预期解除分配。

2)如果您确认只有一个观察者,听起来您可以使用跟踪变量并在第一个通知实例后忽略任何内容。不优雅,但它应该工作。

© www.soinside.com 2019 - 2024. All rights reserved.