MPMusicPlayerController不发布通知

MPMusicPlayerController不发布通知

本文介绍了MPMusicPlayerController不发布通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 MPMusicPlayerController 播放音乐,我还想接收通知 MPMusicPlayerControllerPlaybackStateDidChange 。我设置我的播放器和通知注册几乎就像样例(它的工作,BTW - 它正确接收通知):

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

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

NSLog(@registration 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(@打开播放器通知);
[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);}]; 
解决方案

最后,我想出了答案:播放器必须在主线程上发送消息,它在回想起来有些意义,但它是完全不明显的,直到它曙光我修改了我打开的错误是一个错误的文档,因为我从来没有他们提到玩家必须在主线程上操作。


I"m trying to use the MPMusicPlayerController to play music, and I also want to receive the notification MPMusicPlayerControllerPlaybackStateDidChange. I set up my player and notification registration pretty much just like the sample (which works, BTW - it receives notifications correctly):

- (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];
    }
}

I get a whole lot of bupkis. The methods handle_itemChanged: and handle_stateChanged: are just empty except for an NSLog statement to show they've been hit, and it never appears that they get hit. The NSLog statements in initWithPlaylist: are printed to the log as expected. The above is just a business object in my app. It's not a view or view controller.

Any ideas? The bizarre thing is that the AddMusic sample works just fine for me, and I can't tell that I"m doing anything differently with regard to the MPMusicPlayerController and its notifications.

Update: I've added this line in my app delegate to see the full flood of notifications:

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

I see all kinds of notifications being printed to the console, but none from the media player controller.

解决方案

Finally, I figured out the answer: the player has to be sent messages on the main thread. It makes some sense in retrospect, but it was completely non-obvious until the moment it dawned on me. I modified the bug I opened to be a bug on the documentation because I never did spot where they mentioned that the player must be operated on the main thread.

这篇关于MPMusicPlayerController不发布通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 05:08