From iPhone Development Wiki
Revision as of 18:32, 20 April 2019 by Arandomdev (talk | contribs) (fixed formatting and added removing songs)
Jump to: navigation, search

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

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 */

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