繁体   English   中英

Objective-C Mac OS X分布式通知iTunes

[英]Objective-C Mac OS X Distributed notifications iTunes

我需要一点帮助,我现在有一个方法; 我的Mac OS X应用程序中的updateTrackInfo获取当前在iTunes中播放的艺术家名称,曲目名称和曲目的持续时间

但是,我希望应用程序监听分发的iTunes通知; com.apple.iTunes.playerInfo然后在iTunes分发通知时调用方法updateTrackInfo。 请有人帮助我,我需要在标题和实现文件中写什么。

谢谢,萨米。

您正在寻找-[NSDistributedNotificationCenter addObserver:selector:name:object:]

NSDistributedNotificationCenter *dnc = [NSDistributedNotificationCenter defaultCenter];
[dnc addObserver:self selector:@selector(updateTrackInfo:) name:@"com.apple.iTunes.playerInfo" object:nil];

在同一个班级的其他地方......

- (void) updateTrackInfo:(NSNotification *)notification {
  NSDictionary *information = [notification userInfo];
  NSLog(@"track information: %@", information);
}

它甚至可以在通知中为您提供一大堆跟踪信息。 不是很好吗?

谢谢你的帮助,你帮我纠正了我的代码,我写了这篇文章:

- (id) init {
self = [super init];
if (!self) return nil;
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(receiveNotification:) 
                                             name:@"com.apple.iTunes.playerInfo"
                                           object:nil];
return self;}

- (void) receiveNotification:(NSNotification *) notification {
if ([@"com.apple.iTunes.playerInfo" isEqualToString:@"com.apple.iTunes.playerInfo"]) {
    NSLog (@"Successfully received the test notification!");
}}

但它使用NSNotificationCenter而不是NSDistributedNotificationCenter。 这是我出错的地方。

谢谢,萨米。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM