ios – MPMusicPlayerController不会与Remote Controls交互

即使这个问题被多次询问而且我没有找到任何令人满意的答案,我仍然想再试一次:

我正在尝试使用MPMusicPlayerController播放MPMediaItems.到现在为止还挺好.这是播放来自Itunes Match或Apple Music的文件所必需的.通过AVPlayer播放任何其他文件.

处理使用AVPlayer播放的歌曲的遥控器似乎没有问题:

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

- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent

此外,可以在后台检索AVPlayerItemDidPlayToEndTimeNotification,对此开始播放下一首歌曲.

尝试使用MPMusicPlayerController播放一首歌的所有内容似乎都不能正常工作.我正试着播放这样一首歌:

 self.audioPlayer = [MPMusicPlayerController applicationMusicPlayer];
 self.audioPlayer.repeatMode = MPMusicRepeatModeNone;
 [self.audioPlayer setQueueWithItemCollection:[MPMediaItemCollection collectionWithItems:@[_playerItem]]];

使用给定的观察者:

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playbackStateDidChange:) name:MPMusicPlayerControllerPlaybackStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playbackItemDidChange:) name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:nil];
[self.audioPlayer beginGeneratingPlaybackNotifications];

如果应用程序位于前台,则会按照应有的方式调用观察者.进入背景后,观察者不再被召唤.在这种情况下,我无法检测到歌曲的结尾以跳到下一首歌曲.

此外 – 遥控器不会工作.我用了:

[MPRemoteCommandCenter sharedCommandCenter].playCommand.enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter].playCommand addTarget:self action:@selector(remotePlay)];

[MPRemoteCommandCenter sharedCommandCenter].pauseCommand.enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter].pauseCommand addTarget:self action:@selector(remoteStop)];

[MPRemoteCommandCenter sharedCommandCenter].previousTrackCommand.enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter].previousTrackCommand addTarget:self action:@selector(loadPreviousSong)];

[MPRemoteCommandCenter sharedCommandCenter].nextTrackCommand.enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter].nextTrackCommand addTarget:self action:@selector(loadNextSong)];

但是这些给定的选择器被称为前景和后台.

有没有人能解决我的问题?如上所述我需要使用MPMusicPlayerController,因为我想播放Apple Music和iTunes Match曲目.

相关问题:

> beginReceivingRemoteControlEvents not triggering events for Apple Music
> MPMusicPlayerController breaks lock screen controls

最佳答案 由于音乐应用正在处理项目的实际播放,因此您自己的应用不会收到通知.您最好的办法是通过bugreport.apple.com提交增强请求.

Apple Music / iTunes团队正在听取有关如何改进Apple Music API的反馈.

点赞