MPMusicPlayerController没有发布通知?

Ben*_*ins 2 iphone cocoa-touch ios

我正在尝试使用MPMusicPlayerController播放音乐,我也希望收到通知MPMusicPlayerControllerPlaybackStateDidChange.我设置我的播放器和通知注册就像样本一样(有效,BTW - 它正确接收通知):

- (id) initWithPlaylist:(MPMediaPlaylist*)list {
    if (self = [super init]) {
        player = [MPMusicPlayerController applicationMusicPlayer];
        [player retain];

        NSLog(@"setting up player");
        [plaayer setQueueWithItemCollection:list];
        [player setShuffleMode:MPMusicShuffleModeOff];
        [player setRepeatMode:MPMusicRepeatModeNone];

        NSLog(@"registering MPMusicPlayerController Notifications");
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(handle_itemChanged:) 
                                                     name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification 
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(handle_stateChanged:)
                                                     name:MPMusicPlayerControllerPlaybackStateDidChangeNotification
                                                   object:nil];

        NSLog(@"turning on player notifications");
        [player beginGeneratingPlaybackNotifications];
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到了很多bupkis.这些方法handle_itemChanged:handle_stateChanged:是除了一个是空洞的NSLog声明,表明他们已经被击中,而且也从未出现,他们被打到.将按预期将NSLog语句initWithPlaylist:打印到日志中.以上只是我的应用程序中的业务对象.它不是视图或视图控制器.

有任何想法吗?奇怪的是,AddMusic样本对我来说效果很好,而且我不能说我MPMusicPlayerController对它及其通知做了什么不同的事情.

更新:我在我的应用代理中添加了这一行,以查看大量的通知:

[[NSNotificationCenter defaultCenter] addObserverForName:nil object:nil queue:nil usingBlock:^(NSNotification *n) { NSLog(@"notification: %@", n); }];
Run Code Online (Sandbox Code Playgroud)

我看到各种通知都被打印到控制台,但没有来自媒体播放器控制器.

Erf*_*fan 7

您应该在这些行上方添加另一行:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handle_itemChanged:)
                                             name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
                                           object:nil];
Run Code Online (Sandbox Code Playgroud)

那是:

[myPlayer beginGeneratingPlaybackNotifications];
Run Code Online (Sandbox Code Playgroud)

它对我有用.