MPMusicPlayerController

From iPhone Development Wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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

An 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 */
/* 5000 being a large arbitrary number */
[musicController nowPlayingItemAtIndex:5000];
[requestController beginAutomaticResponseLoading];
[NSThread sleepForTimeInterval:0.05];

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

To remove an item in the queue, Apple uses a system of commands and requests.

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

/* remove the 3rd song */
NSIndexPath *index = [collection indexPathForGlobalIndex:3];
MPCPlayerResponseItem *song = [collection itemAtIndexPath:index];

MPCPlayerCommandRequest *request = [item remove];
[MPCPlayerChangeRequest performRequest:request completion:^{
    /* code */
}];

/* there should be a small delay between consecutive commands */
[NSThread sleepForTimeInterval:0.1];

It is also possible to reorder songs in the queue.

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

/* move the 4th song, after the 0th object (current playing song) */
NSIndexPath *targetSongIndex = [collection indexPathForGlobalIndex:4];
MPCPlayerResponseItem *targetSong = [collection itemAtIndexPath:targetSongIndex];
NSIndexPath *positioningSongIndex = [collection indexPathForGlobalIndex:0];
MPCPlayerResponseItem *positioningSong = [collection itemAtIndexPath:positioningSongIndex];

_MPCPlayerReorderItemsCommand *command = [response.tracklist reorderCommand];
MPCPlayerCommandRequest *request = [command moveItem:targetSong afterItem:positioningSong];
[MPCPlayerChangeRequest performRequest:request completion:nil];