Apple Music API - Create Playlist

I have been exploring the Apple Music API to see what kind of functionality I can expect to be able to use in an iOS app. I have created a little test app that gains permission from the user and outputs the playlists I have (and songs) to

NSLog
.


MPMediaQuery *myPlaylistsQuery = [MPMediaQuery playlistsQuery];

[myPlaylistsQuery setGroupingType:MPMediaGroupingPlaylist];

NSArray *playlists = [myPlaylistsQuery collections];

for (MPMediaPlaylist *playlist in playlists) {

     NSLog (@"%@", [playlist valueForProperty: MPMediaPlaylistPropertyName]);

     NSArray *songs = [playlist items];


     for (MPMediaItem *song in songs) {

          NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];

          NSLog (@"\t\t%@", songTitle);

     }

}


So far, so good. What I want to know is:

is there a way of creating a playlist from my app (via the API)?


I know there is an

MPMediaPlaylist
addItem
and
add
method but can't seem to find a way of creating the new playlist itself. I noticed that Shazam creates a 'My Shazam' playlist when you connect their app to Apple Music so there must be something out there.


According to this page it should be possible: https://affiliate.itunes.apple.com/resources/blog/apple-music-api-faq/


Thanks in advance for any help! I'm going round in circles at the moment

Answered by scgough in 213704022

0down voteaccept



I've figured this out. If you use the following code you can generate a new playlist and perform an action on it.


NSUUID *uuid = [NSUUID UUID];

[[MPMediaLibrary defaultMediaLibrary] getPlaylistWithUUID:uuid creationMetadata:[[MPMediaPlaylistCreationMetadata alloc] initWithName:@"YOUR PLAYLIST NAME"] completionHandler:^(MPMediaPlaylist * _Nullable playlist, NSError * _Nullable error) {

    NSLog(@"%@", error);

    if (!error) { NSLog(@"All ok let's do some stuff with the playlist!"); }

}];
Accepted Answer

0down voteaccept



I've figured this out. If you use the following code you can generate a new playlist and perform an action on it.


NSUUID *uuid = [NSUUID UUID];

[[MPMediaLibrary defaultMediaLibrary] getPlaylistWithUUID:uuid creationMetadata:[[MPMediaPlaylistCreationMetadata alloc] initWithName:@"YOUR PLAYLIST NAME"] completionHandler:^(MPMediaPlaylist * _Nullable playlist, NSError * _Nullable error) {

    NSLog(@"%@", error);

    if (!error) { NSLog(@"All ok let's do some stuff with the playlist!"); }

}];

Would you have to use Object-C/Swift code to use the API to create a playlist or would you be able to use other server-side languages such as Python/PHP/Ruby/etc.?

Apple Music API - Create Playlist
 
 
Q