如果 iPodMusicPlayer 在后台创建,则不会发送通知

发布于 2024-08-31 06:41:12 字数 891 浏览 6 评论 0原文

如果 iPodMusicPlayer 是在后台创建的,那么我不会发送有关播放状态更改的通知。

这是代码:

- (void)initMusicPlayer {
    musicPlayer = [MPMusicPlayerController iPodMusicPlayer];

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self selector:@selector(musicPlayerStateChanged:) 
            name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification 
            object:musicPlayer];
    [notificationCenter addObserver:self selector:@selector(musicPlayerStateChanged:) 
            name:MPMusicPlayerControllerPlaybackStateDidChangeNotification 
            object:musicPlayer];
    [musicPlayer beginGeneratingPlaybackNotifications];
}

- (void)viewDidLoad {
    [self performSelectorInBackground:@selector(initMusicPlayer) withObject:nil];
}

有什么方法可以在后台创建 iPodMusicPlayer 吗?否则,如果它是在主线程上创建的,它会阻塞执行一段时间。

If iPodMusicPlayer is created in background, then I doesn't send notifications about playback state changes.

Here is code:

- (void)initMusicPlayer {
    musicPlayer = [MPMusicPlayerController iPodMusicPlayer];

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self selector:@selector(musicPlayerStateChanged:) 
            name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification 
            object:musicPlayer];
    [notificationCenter addObserver:self selector:@selector(musicPlayerStateChanged:) 
            name:MPMusicPlayerControllerPlaybackStateDidChangeNotification 
            object:musicPlayer];
    [musicPlayer beginGeneratingPlaybackNotifications];
}

- (void)viewDidLoad {
    [self performSelectorInBackground:@selector(initMusicPlayer) withObject:nil];
}

Is there some way how to create iPodMusicPlayer in background? Otherwise if it is created on the main thread, it blocks executions for a while.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文