我有类似以下的代码:
[[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
。第一次之后的值为null
。
谁能告诉我为什么会这样吗?为什么在第一次之后就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];
怎么了。
我想使用代码来重置itemPlayEnded方法内部的Notification,类似于以下内容(p是AVPlayerItem,这是带有Notification的代码中的lastItem):
[[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的完整序列。我终于发现,我在这里尝试做的事情并没有像我想要的那样工作。因此,我最终使用了https://github.com/dgiovann/AVQueuePlayerPrevious中的类,这对我来说非常有用!
您正在预订AVPlayerItemDidPlayToEndTimeNotification,该AVPlayerItemDidPlayToEndTimeNotification首先由您自己发布(带有一些用户信息),然后再次发布,这次不是您自己发布,因此可能包含或可能不包含任何用户信息。请参阅文档,以查看Apple何时以及为何发布此通知,以及可以从中获得哪些信息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句