具有空URL的Objective-C MPMediaItem

问题描述 投票:0回答:2

我有一个项目,利用MPMediaPickerController从媒体播放器中选择音频文件。但是,当试图存储它的URL时,我什么都没有,但null返回。我的代码如下所示:

- (void)showMediaPicker:(NSString *)title {
    MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAny];
    picker.delegate = self;
    picker.prompt = title;
    picker.allowsPickingMultipleItems = NO;
    picker.showsCloudItems = NO;
    [self.viewController presentViewController:picker animated:YES completion:NULL];
}

- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
    MPMediaItem *item = [mediaItemCollection.items firstObject];
    NSURL *assetURL = [item valueForProperty:MPMediaItemPropertyAssetURL];//returning null
    NSString *type = [self contentTypeForFile:assetURL.lastPathComponent];
    NSString *title = [item valueForProperty:MPMediaItemPropertyTitle];
    [self callbackWithName:title type:type url:assetURL];
}

无论出于何种原因,从设备库中选择一个音频项目时,我的*assetURL为零。我可以找到关于这个问题的唯一答案通常与从MediaPicker中选择云项时的网址null有关。但是,如上所述,我设置了showCloudItems = NO

我非常感谢任何帮助;如果您需要任何其他信息,请与我们联系!

objective-c xcode media-player mpmediaitem
2个回答
1
投票

事实证明,来自“iCloud音乐库”的音乐受DRM保护,因此,当在MPMediaPickerController中选择时,assetURL保持为空。简单的解决方法是从“音乐”应用程序的设置中关闭“iCloud音乐库”设置。我希望picker.showsCloudItems = NO;可以防止这种情况,但显然我错了。


0
投票

由于两个原因,MPMediaItemPropertyAssetURL将为null / nil。

  1. 音乐不会下载到您的设备,而只会添加到音乐库中。
  2. 音乐已加载但受DRM保护。

受DRM保护的资产无法使用AVPlayer播放,它只能使用MPMusicPlayer播放。因此,在继续使用AVPlayer之前,您必须先检查两件事。

  1. MPMediaItemPropertyAssetURL是零?
  2. MPMediaItem受到保护?

请看下面的代码......

MPMediaItem *theChosenSong = [[mediaItemCollection items] firstObject];
NSURL *assetURL = [theChosenSong valueForProperty:MPMediaItemPropertyAssetURL];

            if(assetURL) {
                BOOL bIsProtected = theChosenSong.protectedAsset;
                if(!bIsProtected) {
                    // Do whatever you want to do
                    NSLog(@"Its not protected");
               }
                else {
                    NSLog(@"Its DRM protected");
                }
            }
            else {
                    NSLog(@"Its DRM protected");
            }
© www.soinside.com 2019 - 2024. All rights reserved.