Ios 如何在第一次访问my@selector(方法)后获取非空userInfo

Ios 如何在第一次访问my@selector(方法)后获取非空userInfo,ios,objective-c,nsnotificationcenter,Ios,Objective C,Nsnotificationcenter,我有如下代码: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemPlayEnded:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil]; [[NSNotificationCenter defaultCenter] postNotificationName:AVPlayerItemDidPlayToEndTimeNotif

我有如下代码:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemPlayEnded:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];

[[NSNotificationCenter defaultCenter] postNotificationName:AVPlayerItemDidPlayToEndTimeNotification object:nil userInfo:userInfo];
在选择器方法中:

- (void)itemPlayEnded:(NSNotification *)notification
{
    NSLog(@"Entered itemPlayEnded");

    AVPlayerItem *p = [notification object];
    NSLog(@"userinfo description %@",[[notification userInfo] description]);
}
第一次访问itemPlayEnded时,userInfo为非空。第一次之后,该值为null

有谁能告诉我为什么会这样?为什么第一次之后userInfo的值为空

编辑:

我需要澄清发生了什么。我还更新了我的通知代码以使用lastItem,它是一个AVPlayerItem

queuePlayer = [[AVQueuePlayer alloc] init];
NSDictionary *userInfo = @{@"tones": copyoftones};

for (NSString *playThis in listOfTonesToBePlayed) {
    NSString *soundPath =[[NSBundle mainBundle] pathForResource:playThis ofType:@"mp3"];
    NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
    AVPlayerItem *thePlayerItemA = [[AVPlayerItem alloc] initWithURL:soundURL];
    lastItem = thePlayerItemA;
    [queuePlayer insertItem:thePlayerItemA afterItem:nil];
}
queuePlayer.actionAtItemEnd = AVPlayerActionAtItemEndAdvance;


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemPlayEnded:) name:AVPlayerItemDidPlayToEndTimeNotification object:lastItem];

[[NSNotificationCenter defaultCenter] postNotificationName:AVPlayerItemDidPlayToEndTimeNotification object:lastItem userInfo:userInfo];

[queuePlayer play];
发生了什么

在queuePlayer能够播放音调之前,输入itemPlayEnded以及字典non nil 接下来播放音调列表 使用字典nil重新输入项目playended。 我想使用代码在itemPlayEnded方法中重置通知,如下所示:p是AVPlayerItem,它是带有通知的代码中的最后一个项:

[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:p];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemPlayEnded:) name:AVPlayerItemDidPlayToEndTimeNotification object:p];        
[[NSNotificationCenter defaultCenter] postNotificationName:AVPlayerItemDidPlayToEndTimeNotification object:p userInfo:userInfo];

[queuePlayer play];
但是,itemPlayEnded方法将在不播放播放器的情况下重新输入到一个永无止境的循环中。比赛条件

有什么建议吗

编辑2: 我已确定违规代码为:

[[NSNotificationCenter defaultCenter] postNotificationName:AVPlayerItemDidPlayToEndTimeNotification object:lastItem userInfo:userInfo];
如何设置对象以使用正确的发送器。还不知道该放什么

编辑3:


我真正想做的是能够循环播放完整的声音或MP3序列。我终于意识到我在这里所做的事情不会像我想要的那样奏效。因此,我最终使用了来自的课程,这对我来说非常好

您正在订阅AvPlayerItemDidPlaytoEndTime通知,该通知首先由您自己发布,包含一些用户信息,然后再次发布,这次不是由您发布,因此可能包含也可能不包含任何用户信息。请参阅文档,了解苹果何时以及为何发布此通知,以及您可以从中获得哪些信息


您是否检查了通知海报是否通过了非零字典?是的,第一次是非零,第一次是零。也许有人可以向我解释为什么?那么,其他时候它是如何被调用的呢?正如CrimsonChris在下面提到的,它是通过AVPlayerItemDidPlayToEndTimeNotification调用的。在播放声音文件的末尾。我将接受CrimsonChris的建议,并阅读更多关于如何调用AVPlayerItemDidPlayToEndTimeNotification的信息。我希望它在每次通话中使用相同的用户信息,但似乎不是。我正在计算答案,尽管我最终使用了不同的方法来获得结果。