MPMusicPlayerController: Difference between revisions

From iPhone Development Wiki
(fixed formatting and added removing songs)
(updated some code due to further research)
 
Line 17: Line 17:


/* Inorder to access the current queue that is playing, theses method calls are necessary */
/* Inorder to access the current queue that is playing, theses method calls are necessary */
NSInteger numberOfItems = [musicController numberOfItems];
/* 5000 being a large arbitrary number */
[musicController nowPlayingItemAtIndex:numberOfItems];
[musicController nowPlayingItemAtIndex:5000];
[requestController setNeedsReloadForSignificantRequestChange];
[requestController beginAutomaticResponseLoading];
[requestController beginAutomaticResponseLoading];
[NSThread sleepForTimeInterval:0.05];
</syntaxhighlight>
</syntaxhighlight>


Line 54: Line 54:
     /* code */
     /* code */
}];
}];
/* there should be a small delay between consecutive commands */
[NSThread sleepForTimeInterval:0.1];
</syntaxhighlight>
</syntaxhighlight>



Latest revision as of 12:55, 12 July 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

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