Apple Music与MPNowPlayingInfoCenter冲突

Mar*_*tti 7 notifications objective-c ios mpnowplayinginfocenter

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

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

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

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

当我AVPlayer与Apple Music SPTAudioStreamingController同步并与Spotify同步时,我正在使用它来播放音乐.

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

- (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];
        }];
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是事件处理的代码:

- (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];
    }
}
Run Code Online (Sandbox Code Playgroud)

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