Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when using -[MPMediaPlaylist addItemWithProductID:completionHandler:]

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!

like image 701
Jota Avatar asked Apr 27 '16 23:04

Jota


1 Answers

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.

like image 105
FKDev Avatar answered Oct 16 '22 15:10

FKDev