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