6

I'm trying to use the new Apple Music APIs from 9.3 to add a song to a playlist created by my app, without adding it to the user's library.

Consider the productID 316654632, it's the song Lisztomania by Phoenix, in the US iTunes Store.

Using the following code, I can play the song

MPMusicPlayerController *musicPlayer = [MPMusicPlayerController systemMusicPlayer];  
[musicPlayer setQueueWithStoreIDs:@[@"316654632"]];  
[musicPlayer play];  

Using the following code, I can add the song to my Apple Music library

[[MPMediaLibrary defaultMediaLibrary] addItemWithProductID:@"316654632" completionHandler:^(NSArray<__kindof MPMediaEntity *> * _Nonnull entities, NSError * _Nullable error) {  
    NSLog(@"%@", error);  
}];  

Error is nil, and I can see the song in my library.

But trying the same with a playlist doesn't work.

[[MPMediaLibrary defaultMediaLibrary] getPlaylistWithUUID:uuid creationMetadata:[[MPMediaPlaylistCreationMetadata alloc] initWithName:@"Test Playlist"] completionHandler:^(MPMediaPlaylist * _Nullable playlist, NSError * _Nullable error) {  
    NSLog(@"%@", error);

    if (!error) {  
        [playlist addItemWithProductID:@"316654632" completionHandler:^(NSError * _Nullable error) {  
            NSLog(@"%@", error);  
        }];  
    }  
}];  

The playlist is created, I can see it in Music.app, but when I try to add the same product ID I played & added to my library to the playlist, I get an error

Error Domain=MPErrorDomain Code=4 "The requested id could not be found" UserInfo={NSLocalizedDescription=The requested id could not be found}

But how could it not be found if I successfully added the same item to my library?

UPDATE

Good news! Apple has fixed rdar://26408683 on 10.2.1!

Jota
  • 149
  • 3
  • 9
  • any luck on finding a solution? – TWilly May 01 '16 at 13:51
  • @TWilly nope :( I even posted it on the Apple dev forums and no replies. – Jota May 01 '16 at 19:57
  • I had this problem too, used -[MPMediaPlaylist addMediaItems:completionHandler:] as a workaround. – Dan May 03 '16 at 18:08
  • 1
    @Dan sure, that works, but that way I have to add the song to my library, right? That's exactly what I don't want, I want to add a song to a playlist without adding it to the user's library (which is a recent feature in Apple Music) – Jota May 05 '16 at 03:51
  • Yes, it looks that way. On the other hand, you have just answered my question as to why some of my playlist tracks won't trigger MPMediaLibraryDidChangeNotification. – Dan May 05 '16 at 14:06
  • We have the same problem... However Soundcloud is able to do it and our users are complaining about that we add tracks to main lib. Why Apple, just why? – Michał Hernas May 21 '16 at 19:10
  • Well, I've filed rdar://26408683 – Jota May 22 '16 at 03:18

1 Answers1

4

In my playlist conversion app (mixlib), the only solution I have found to reliably add some tracks to a newly created playlist is to wait.

In my tests, waiting five seconds seems to be enough.

[[MPMediaLibrary defaultMediaLibrary] getPlaylistWithUUID:uuid creationMetadata:[[MPMediaPlaylistCreationMetadata alloc] initWithName:@"Test Playlist"] completionHandler:^(MPMediaPlaylist * _Nullable playlist, NSError * _Nullable error) {  
if (!error) {  
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 /*seconds*/ * NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)), ^() {
        [playlist addItemWithProductID:@"316654632" completionHandler:^(NSError * _Nullable error) {  
            NSLog(@"%@", error);  
    }];  
}  
}];  

I suspect it is a server/network related issue because it sometimes works without waiting. The "requested id" which is not found may be the playlist id, not the track id.

When it starts to work for a playlist, then it will always work. So you don't need to wait before adding each additional track, but only before adding the first one.

FKDev
  • 2,266
  • 1
  • 20
  • 23