2

I am having a super strange issue. I am using iOS13 MPMediaPickerController to get some songs from the users apple music library in order to play it via the [MPMusicPlayerController applicationMusicPlayer].

Now with some non-downloaded songs from some playlists that I have added to my library the delegate function

- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection

returns for

mediaItemCollection.items.firstObject

an object of the type MPModelObjectMediaItem which cannot be played by the player. (I can also not cast to that type because it's unknown. Also there is no documentation out there for this type. It seems to be a private class inside the mediaplayer framework.)

I am expecting the type MPConcreteMediaItem which is incoming for all downloaded (and all other cloud items that are not in playlists)

The picker is configured like this

musicPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
musicPicker.showsCloudItems               = true;
musicPicker.showsItemsWithProtectedAssets = true;
musicPicker.delegate                      = self;
musicPicker.allowsPickingMultipleItems    = false;

Any tips whats going on there?

Martin Mlostek
  • 2,755
  • 1
  • 28
  • 57

1 Answers1

2

Well, it seems that items that you load into your own application cannot be properly loaded if you haven't added them into your music library. (iOS13.3)

I am using this workaround:

- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
    assert(mediaItemCollection.count == 1);
    // this check is done because it seems that in the following scenario you dont get a
    // MPConcreteMediaItem which can be played. Instead you get a MPModelObjectMediaItem
    // this happens when an item was selected that is inside a playlist, but does not belong
    // to your library (nor is it downloaded to your device)
    if(mediaItemCollection.items.firstObject.persistentID <= 0)
    {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Song Not Added")
                                                                message:@"This song must be added to your Apple Music library. Please add the song to your library from the Music App."
                                                                preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"Dismiss"
                                                  style:UIAlertActionStyleDefault
                                                  handler:nil]];
        [alertController addAction:[UIAlertAction actionWithTitle:@"Open Music App"
                                                  style:UIAlertActionStyleCancel
                                                  handler:^(UIAlertAction *alertAction)
                                                  {
                                                      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"music://"]
                                                                      options:nil
                                                                      completionHandler:nil];
                                                  }]];
        [self presentViewController:alertController animated:true completion:nil];
        return;
    }
    // good to go? then load it
    [musicPlayer setQueueWithItemCollection:mediaItemCollection];
    ...
}
Martin Mlostek
  • 2,755
  • 1
  • 28
  • 57