Search code examples
iphonemultithreadingmpmusicplayercontroller

iPodMusicPlayer doesn't send notifications, if it is created in background


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.


Solution

  • A music player must be used on main thread.

    https://developer.apple.com/library/ios/#documentation/MediaPlayer/Reference/MPMusicPlayerController_ClassReference/Reference/Reference.html%23//apple_ref/doc/uid/TP40008221