我有一个 super 奇怪的问题。我正在使用iOS13 MPMediaPickerController从用户Apple音乐库中获取一些歌曲,以便通过[MPMusicPlayerController applicationMusicPlayer]播放它。

现在,从我添加到库中的一些播放列表中下载了一些未下载的歌曲后,委托函数

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

回报
mediaItemCollection.items.firstObject

播放器无法播放的MPModelObjectMediaItem类型的对象。 (我也不能转换为该类型,因为它是未知的。也没有关于该类型的文档。它似乎是mediaplayer框架内的一个 private 类。)

我期望所有下载的(以及不在播放列表中的所有其他云项目)传入的MPConcreteMediaItem类型

选择器的配置如下
musicPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
musicPicker.showsCloudItems               = true;
musicPicker.showsItemsWithProtectedAssets = true;
musicPicker.delegate                      = self;
musicPicker.allowsPickingMultipleItems    = false;

有什么提示吗?

最佳答案

好吧,如果您尚未将加载到自己的应用程序中的项目添加到音乐库中,则似乎无法正确加载。 (iOS13.3)

我正在使用此解决方法:

- (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];
    ...
}

10-08 07:30