[英]MPMusicPlayerController not posting notifications?
我正在尝试使用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];
}
}
我得到了很多bupkis。 方法handle_itemChanged:
和handle_stateChanged:
只是空的,除了NSLog
语句以显示它们已被命中,并且它们似乎永远不会被命中。 initWithPlaylist:
的NSLog
语句initWithPlaylist:
按预期打印到日志中。 以上只是我的应用程序中的业务对象。 它不是视图或视图控制器。
有任何想法吗? 奇怪的是, AddMusic
示例对我来说效果很好,我不能说我对MPMusicPlayerController
及其通知做了不同的事情。
更新 :我在我的应用代理中添加了这一行,以查看大量的通知:
[[NSNotificationCenter defaultCenter] addObserverForName:nil object:nil queue:nil usingBlock:^(NSNotification *n) { NSLog(@"notification: %@", n); }];
我看到各种通知都被打印到控制台,但没有来自媒体播放器控制器。
您应该在这些行上方添加另一行:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handle_itemChanged:)
name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
object:nil];
那是:
[myPlayer beginGeneratingPlaybackNotifications];
它对我有用。
最后,我想出了答案:玩家必须在主线程上发送消息。 回想起来它有一定道理,但直到它突然出现在我身上时才完全不明显。 我修改了我打开的bug是文档中的一个bug,因为我从来没有发现他们提到玩家必须在主线程上运行的地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.