Difference between revisions of "MPMusicPlayerController"

From iPhone Development Wiki
Jump to: navigation, search
(created page)
(No difference)

Revision as of 18:15, 20 April 2019


Described by apple, the MPMusicPlayerController class is, "An object used to play audio media items from the device's Music app library." [1]

Using the systemMusicPlayer property, you can control and access several things, including but not limited to, play, pause, shuffle, adding items, and getting the current playing item, all of these are documented here.

MPMusicPlayerController *controller = [MPMusicPlayerController systemMusicPlayer];
MPMediaItem *currentSong = [controller nowPlayingItem];

Private Methods

A undocumented property of the systemMusicPlayer is the requestController, using this object, you can control the queue shown in the music app.

MPMusicPlayerController *musicController = [MPMusicPlayerController systemMusicPlayer];
MPRequestResponseController *requestController = [musicController requestController];

/* Inorder to access the current queue that is playing, theses method calls are necessary */
NSInteger numberOfItems = [musicController numberOfItems];
[musicController nowPlayingItemAtIndex:numberOfItems];
[requestController setNeedsReloadForSignificantRequestChange];
[requestController beginAutomaticResponseLoading];

Then to get the queue, iterate though the queue with the total number of songs.

MPCPlayerResponseTracklist *tracklist = responseController.tracklist;
MPSectionedCollection *collection = tracklist.items;

/* The collection includes an extra entry, subtracting by one returns the correct number of songs */
NSInteger total = [collection totalItemCount] - 1;
NSInteger nowPlayingOffset = (NSInteger)[musicController indexOfNowPlayingItem];

for (size_t index = 0; index < total; index++) {
    /* The current playing song is positioned at 0 */
    NSInteger indexOfSong = nowPlayingOffset + index;
    MPMediaItem *song = [musicController nowPlayingItemAtIndex:indexOfSong];