Brief stop/start of Now Playing item when updating a media collection queue MPMediaItemCollection

╄→гoц情女王★ 提交于 2019-12-10 20:04:45

问题


Im playing about with apples addMusic sample app in xcode, as im looking to be able to add similar functionality (playing a queue of music selected by the user in the background of the app).

It does this annoying thing where the music player briefly stops before playing again when the user hits the 'Done' button after picking their song selections in the built in MediaPicker. - I'm assuming this is because of the method apple used, which applies a new array as the queue, and resets the now playing state as to where it left off (like so:)

        // 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];
    }
}

}

Anyone experienced this issue? Is there any alternative/more efficient methods that anyone can think of?

Thanks in advance for sharing :)


回答1:


Simply apply the changes to MPMediaItemCollection at the point of Music Player changed notification.



来源:https://stackoverflow.com/questions/13109200/brief-stop-start-of-now-playing-item-when-updating-a-media-collection-queue-mpme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!