4

私は少し助けが必要です、私は現在方法を持っています。Mac OS XアプリケーションのupdateTrackInfoは、iTunesで現在再生されているアーティスト名、トラック名、およびトラックの長さを取得します。

ただし、アプリに配布されたiTunes通知をリッスンさせたい。次に、 com.apple.iTunes.playerInfoは、通知がiTunesによって配信されるたびに、メソッドupdateTrackInfoを呼び出します。ヘッダーと実装ファイルの両方に何を書き込む必要があるかについて、誰かが私を助けてくれませんか。

ありがとう、サミ。

4

2 に答える 2

13

あなたが探している-[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);
}

それはあなたに通知であなたにたくさんのトラック情報さえ与えます。いいじゃないですか?

于 2011-02-03T17:37:44.187 に答える
3

あなたの助けに感謝します、あなたは私が私のコードを修正するのを手伝ってくれました、私はこれを書き留めました:

- (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!");
}}

ただし、NSDistributedNotificationCenterの代わりにNSNotificationCenterを使用しました。それは私が間違っていたところです。

ありがとう、サミ。

于 2011-02-03T17:56:14.130 に答える