MPMusicPlayerController breaks lock screen control

2020-06-27 01:51发布

问题:

I'm attempting to use MPMusicPlayerController to play apple music songs, but I can't get the lock screen controls to work. It seems as though MPMusicPlayerController overridess the remoteControlReceivedWithEvent listener.

Here is how I set up my controller:

self.player = [MPMusicPlayerController applicationMusicPlayer];
self.player.repeatMode = MPMusicRepeatModeNone;
self.player.shuffleMode = MPMusicShuffleModeOff;
[self.player beginGeneratingPlaybackNotifications];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlePlaybackStateChanged:) name:MPMusicPlayerControllerPlaybackStateDidChangeNotification object:self.player ];


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleItemChanged:) name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:self.player ];

Then I play apple music songs:

NSMutableArray *storeIDS = [NSMutableArray arrayWithObjects:anthem.song.apple_id, @"1", nil];

[self.player setQueueWithStoreIDs:storeIDS];
[self.player play];
[self.player setCurrentPlaybackRate:1.0];

For reference, here is how I setup the remote control listener in didFinishLaunchingWithOptions:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];

At this point, the player plays the song as requested, but I can no longer receive any remote control notifications. Hitting next/prev simply stop the song since it has reached the end of the list. I've tried with the applicationMusicPlayer as well as the systemMusicPlayer. I can't use AVPlayer or AVAudioPlayer because it's Apple Music and I cannot get the URL to stream.

Any ideas!?

回答1:

To play from Apple Music use MPMusicPlayerController.systemMusicPlayer()