Difference between revisions of "MPMusicPlayerController"

From iPhone Development Wiki
Jump to: navigation, search
(created page)
 
(fixed formatting and added removing songs)
Line 1: Line 1:
 
== MPMusicPlayerController ==
 
 
Described by apple, the '''MPMusicPlayerController''' class is, "An object used to play audio media items from the device's Music app library." [https://developer.apple.com/documentation/mediaplayer/mpmusicplayercontroller?language=objc]
 
Described by apple, the '''MPMusicPlayerController''' class is, "An object used to play audio media items from the device's Music app library." [https://developer.apple.com/documentation/mediaplayer/mpmusicplayercontroller?language=objc]
  
Line 12: Line 10:
  
 
=== Private Methods ===
 
=== Private Methods ===
A undocumented property of the systemMusicPlayer is the requestController, using this object, you can control the queue shown in the music app.
+
An undocumented property of the systemMusicPlayer is the requestController, using this object, you can control the queue shown in the music app.
  
 
<syntaxhighlight lang = "Objective-C">
 
<syntaxhighlight lang = "Objective-C">
Line 40: Line 38:
 
     MPMediaItem *song = [musicController nowPlayingItemAtIndex:indexOfSong];
 
     MPMediaItem *song = [musicController nowPlayingItemAtIndex:indexOfSong];
 
}
 
}
 +
</syntaxhighlight>
 +
 +
To remove an item in the queue, Apple uses a system of commands and requests.
 +
 +
<syntaxhighlight lang = "Objective-C">
 +
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 */
 +
}];
 +
</syntaxhighlight>
 +
 +
It is also possible to reorder songs in the queue.
 +
<syntaxhighlight lang = "Objective-C">
 +
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];
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 18:32, 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

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