Error when using -[MPMediaPlaylist addItemWithProductID:completionHandler:]

Consider the productID 316654632, it's the song Lisztomania by Phoenix, in the US 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);
}];

This prints "nil", no error there, and the song is 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); // this prints nil, no error, I got the playlist

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

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


What could be the problem? Is there a different ID I should use for the playlist? Or maybe I can't add a song to a playlist without adding it to my library? (really hope this is not the case)

Accepted Reply

In my playlist conversion app (mixlib on the app store), 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.

Also, you must not block the completionhandler (use dispatch_after for example) :


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


However, just yesterday, during my tests, I had some "code 4" errors for an hour and then it worked again.


So, I am afraid I will have to display an error to the user along the line of "Apple server are unreachable, please retry later" to prevent raging one star reviews.

Replies

No one? 😐

In my playlist conversion app (mixlib on the app store), 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.

Also, you must not block the completionhandler (use dispatch_after for example) :


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


However, just yesterday, during my tests, I had some "code 4" errors for an hour and then it worked again.


So, I am afraid I will have to display an error to the user along the line of "Apple server are unreachable, please retry later" to prevent raging one star reviews.