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,1024,1024)];[thisReplacementVIEw.layer addSublayer: animatedLayer];// Begin playback[controlledplayer play];// Clear some content[self displayNoContent];pageContent = theString;playingStatus = YES;
}
– (voID)itemDIDFinishPlaying {
[self displayContent: pageContent];
}
解决方法 试试这个,-(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];总结
以上是内存溢出为你收集整理的ios – NSNotification中的观察者(itemDidFinishPlaying)随机调用两次全部内容,希望文章能够帮你解决ios – NSNotification中的观察者(itemDidFinishPlaying)随机调用两次所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)