Objective c 更新媒体收集队列MPMediaItemCollection时,立即播放项目的短暂停止/开始

Objective c 更新媒体收集队列MPMediaItemCollection时,立即播放项目的短暂停止/开始,objective-c,xcode,mpmediaitemcollection,Objective C,Xcode,Mpmediaitemcollection,我在xcode中使用Apple addMusic示例应用程序,因为我希望能够添加类似的功能(在应用程序的后台播放用户选择的音乐队列) 当用户在内置的MediaPicker中选择歌曲后点击“完成”按钮时,音乐播放器会在再次播放之前短暂停止播放,这是一件令人讨厌的事情。 -我假设这是因为apple使用了一种方法,该方法将一个新数组作为队列,并将当前播放状态重置为停止播放的位置(如:) 有人经历过这个问题吗?是否有任何人都能想到的替代/更有效的方法 提前感谢您的分享:)只需在音乐播放器更改通知点将更改

我在xcode中使用Apple addMusic示例应用程序,因为我希望能够添加类似的功能(在应用程序的后台播放用户选择的音乐队列)

当用户在内置的MediaPicker中选择歌曲后点击“完成”按钮时,音乐播放器会在再次播放之前短暂停止播放,这是一件令人讨厌的事情。 -我假设这是因为apple使用了一种方法,该方法将一个新数组作为队列,并将当前播放状态重置为停止播放的位置(如:)

有人经历过这个问题吗?是否有任何人都能想到的替代/更有效的方法


提前感谢您的分享:)

只需在音乐播放器更改通知点将更改应用于MPMediaItemCollection即可。

问题是您在后台时没有收到通知。似乎唯一的方法是立即执行。如果您在调用setQueueWithItemCollection之前暂停播放器,则静音不太明显。
        // apply the new media item collection as a playback queue for the music player
        [self setUserMediaItemCollection: mediaItemCollection];
        [musicPlayer setQueueWithItemCollection: userMediaItemCollection];
        [self setPlayedMusicOnce: YES];
        [musicPlayer play];

        // Obtain the music player's state so it can then be
        //      restored after updating the playback queue.
    } else {

        // Take note of whether or not the music player is playing. If it is
        //      it needs to be started again at the end of this method.
        BOOL wasPlaying = NO;
        if (musicPlayer.playbackState == MPMusicPlaybackStatePlaying) {
            wasPlaying = YES;
        }

        // Save the now-playing item and its current playback time.
        MPMediaItem *nowPlayingItem         = musicPlayer.nowPlayingItem;
        NSTimeInterval currentPlaybackTime  = musicPlayer.currentPlaybackTime;

        // Combine the previously-existing media item collection with the new one
        NSMutableArray *combinedMediaItems  = [[userMediaItemCollection items] mutableCopy];
        NSArray *newMediaItems              = [mediaItemCollection items];
        [combinedMediaItems addObjectsFromArray: newMediaItems];

        [self setUserMediaItemCollection: [MPMediaItemCollection collectionWithItems: (NSArray *) combinedMediaItems]];

        // Apply the new media item collection as a playback queue for the music player.
        [musicPlayer setQueueWithItemCollection: userMediaItemCollection];

        // Restore the now-playing item and its current playback time.
        musicPlayer.nowPlayingItem          = nowPlayingItem;
        musicPlayer.currentPlaybackTime     = currentPlaybackTime;

        // If the music player was playing, get it playing again.
        if (wasPlaying) {
            [musicPlayer play];

        }
    }


}
}

// If the music player was paused, leave it paused. If it was playing, it will continue to
//      play on its own. The music player state is "stopped" only if the previous list of songs
//      had finished or if this is the first time the user has chosen songs after app
//      launch--in which case, invoke play.

- (void) restorePlaybackState {

if (musicPlayer.playbackState == MPMusicPlaybackStateStopped && userMediaItemCollection) {

    if (playedMusicOnce == NO) {

        [self setPlayedMusicOnce: YES];
        [musicPlayer play];
    }
}

}