Apple Music与MPNowPlayingInfoCenter冲突

当我的音乐播放器应用在背景上播放时,我需要一些问题的帮助。

我可以在应用程序和后台播放这两种服务的音乐。 我也可以设置MPNowPlayingInfoCenter并显示正确的信息,但只有当用户使用Spotify进行身份validation时,播放/暂停,下一首曲目和之前的曲目才有效:

  • 当用户使用Spotify进行身份validation时,应用程序会正确接收通知

  • 但是当用户使用Apple Music进行身份validation时,它不起作用。 在这种情况下,Apple Music似乎是接收通知的人。

当我与Spotify同步时,我正在使用AVPlayer播放与Apple Music和SPTAudioStreamingController同步时的音乐。

以下是媒体中心设置的代码:

 - (void)setMediaCenterinfoForPlayer:(id)player { SPTAudioStreamingController *spotifyPlayer; AVPlayer *localPlayer; NSMutableDictionary *trackInfo = [[NSMutableDictionary alloc] initWithDictionary: @{ MPMediaItemPropertyTitle: self.currentTrack.name, MPMediaItemPropertyArtist: ((SPTArtist *)self.currentTrack.artists[0]).name, MPMediaItemPropertyAlbumTitle : self.currentTrack.album.name, MPNowPlayingInfoPropertyPlaybackRate: @(1.0) }]; if ([player isKindOfClass:[SPTAudioStreamingController class]]) { spotifyPlayer = (SPTAudioStreamingController *)player; [trackInfo setObject:[NSNumber numberWithFloat:spotifyPlayer.currentPlaybackPosition] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]; [trackInfo setObject:[NSNumber numberWithFloat:spotifyPlayer.currentTrackDuration] forKey:MPMediaItemPropertyPlaybackDuration]; } else { localPlayer = (AVPlayer *)player; NSTimeInterval playbackTime = [self currentPlaybackTimeForPlayer:player]; [trackInfo setObject:@(playbackTime) forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]; [trackInfo setObject:@(CMTimeGetSeconds(localPlayer.currentItem.asset.duration)) forKey:MPMediaItemPropertyPlaybackDuration]; } [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = trackInfo; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter"); if (playingInfoCenter) { [self albumURLCoverForCurrentTrackWithBlock:^(UIImage *albumImage) { MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:albumImage]; [trackInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork]; [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:trackInfo]; }]; } } 

以下是事件处理的代码:

 - (void)remoteControlReceivedWithEvent:(UIEvent *)event { if (event.type == UIEventTypeRemoteControl) { MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter]; NSMutableDictionary *playingInfo = [NSMutableDictionary dictionaryWithDictionary:center.nowPlayingInfo]; [playingInfo setObject:[NSNumber numberWithFloat:[AudioPlayerManager sharedInstance].spotifyPlayer.currentPlaybackPosition] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]; center.nowPlayingInfo = playingInfo; if (event.subtype == UIEventSubtypeRemoteControlPlay) { [[AudioPlayerManager sharedInstance] playTrack]; } else if (event.subtype == UIEventSubtypeRemoteControlPause) { [[AudioPlayerManager sharedInstance] pauseTrack]; } else if (event.subtype == UIEventSubtypeRemoteControlPreviousTrack) { [[AudioPlayerManager sharedInstance] previousTrack]; } else if (event.subtype == UIEventSubtypeRemoteControlNextTrack) { [[AudioPlayerManager sharedInstance] nextTrack]; } [[NSNotificationCenter defaultCenter] postNotificationName:kEventTypeRemoteControlUpdateState object:self]; } } 

谁能指点我解决这种情况的方法?