我有一个项目,利用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
。
我非常感谢任何帮助;如果您需要任何其他信息,请与我们联系!
事实证明,来自“iCloud音乐库”的音乐受DRM保护,因此,当在MPMediaPickerController中选择时,assetURL保持为空。简单的解决方法是从“音乐”应用程序的设置中关闭“iCloud音乐库”设置。我希望picker.showsCloudItems = NO;
可以防止这种情况,但显然我错了。
由于两个原因,MPMediaItemPropertyAssetURL将为null / nil。
受DRM保护的资产无法使用AVPlayer播放,它只能使用MPMusicPlayer播放。因此,在继续使用AVPlayer之前,您必须先检查两件事。
请看下面的代码......
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");
}