我需要一点帮助,我目前有一个方法;我的 Mac OS X 应用程序中的 updateTrackInfo 获取艺术家姓名、曲目名称和当前在 iTunes 中播放的曲目的持续时间
但是我希望应用程序监听分发的 iTunes 通知;com.apple.iTunes.playerInfo然后在 iTunes 分发通知时调用方法 updateTrackInfo。请有人帮助我,我需要在头文件和实现文件中写什么。
谢谢,萨米。
我需要一点帮助,我目前有一个方法;我的 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。这就是我出错的地方。
谢谢,萨米。