4

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

From this, I have been able to deduce the following (but I'm not 100% certain):

  • the playlist (basic info: name, id) is stored locally on the device
  • the playlist songs are also pulled from local storage but if the playlist hasn't been downloaded to the device it goes off to Apple to grab the song list.

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.

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

Can a developer create brand new playlists on the user’s device with the Apple Music API?

Yes. The API allows develops to new create playlists on the user’s device.

Community
  • 1
  • 1
scgough
  • 5,099
  • 3
  • 30
  • 48

1 Answers1

5

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]; //uuid for the playlist
[[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!"); 
    }
}];

Apple's documentation on the whole API is severely lacking in terms of sample code and practical examples!

scgough
  • 5,099
  • 3
  • 30
  • 48
  • Hi, I know it is almost 2 years later but still there are no objective-c (one swift one but I am too old to learn a new language :) sample apps to access Apple Music. You would be my hero if you could share yours with me somehow. – KiloOne Dec 19 '18 at 13:47
  • If that is not possible can you help out here? https://stackoverflow.com/questions/53853478/error-using-skcloudservicecontroller-api-to-check-apple-music-capabilities – KiloOne Dec 19 '18 at 15:30