【发布时间】:2010-10-11 08:49:59
【问题描述】:
我正在尝试使用MPMusicPlayerController 播放音乐,并且我也想收到通知MPMusicPlayerControllerPlaybackStateDidChange。我设置播放器和通知注册的方式与示例非常相似(顺便说一句,它可以工作)正确接收通知):
- (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];
}
}
我得到了很多 bupkis。方法 handle_itemChanged: 和 handle_stateChanged: 只是空的,除了 NSLog 声明它们已被击中,而且看起来它们从未被击中。 initWithPlaylist: 中的 NSLog 语句按预期打印到日志中。以上只是我的应用程序中的一个业务对象。它不是视图或视图控制器。
有什么想法吗?奇怪的是,AddMusic 示例对我来说工作得很好,我不能说我对MPMusicPlayerController 及其通知做了什么不同的事情。
更新:我在我的应用代理中添加了这一行,以查看全部通知:
[[NSNotificationCenter defaultCenter] addObserverForName:nil object:nil queue:nil usingBlock:^(NSNotification *n) { NSLog(@"notification: %@", n); }];
我看到各种通知都打印到控制台,但没有来自媒体播放器控制器。
【问题讨论】:
-
aarrrgggh!这是疯子!我仍然不明白为什么没有发布通知。
标签: cocoa-touch ios iphone