7

当我的音乐播放器应用在后台播放时,我需要一些帮助来解决问题。

我可以使用这两种服务在应用程序和后台播放音乐。我也可以设置MPNowPlayingInfoCenter它并显示正确的信息,但播放/暂停、下一首曲目和上一首曲目仅在用户通过 Spotify 身份验证时才有效:

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

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

当与 Apple Music同步以及与 Spotify 同步时,我正在使用AVPlayer播放音乐。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];
    }
}

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

4

0 回答 0