Arm*_*and 6 objective-c uitextview nsnotification ios avplayer
视频播放完毕后,我正在显示文本.我正在使用通知技术来实现这一目标.唯一的问题是Observer每隔一段时间被调用两次.它触发"itemDidFinishPlaying"两次(因此同名的方法).我无法预测何时.我不知道为什么.它看起来是随机的(我知道这听起来很奇怪)就好像它工作得很好让我们说连续15次,然后下一次这种行为突然发生了.我做了一个重建并运行应用程序,这次它连续运行19次,然后两次调用Observer等等......不可预测.我已经尝试过每个场景来预测bug以便修复它.到目前为止,这是不可能的.所以我有2个问题.
1)为什么会发生并"随机"?
2)如何解决这个双重调用问题?
这两个以下的对话也没有帮助:
为什么NSNotification中的Observer调用了两次....?
如何阻止NSNotification中的Observer调用两次?
请在下面找到我的代码:
- (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;
Run Code Online (Sandbox Code Playgroud)
}
- (void)itemDidFinishPlaying {
[self displayContent: pageContent];
Run Code Online (Sandbox Code Playgroud)
}
试试这个,
-(void)itemDidFinishPlaying {
[self displayContent: pageContent];
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
}
Run Code Online (Sandbox Code Playgroud)
它可能适合你.
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];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4158 次 |
| 最近记录: |