Justin Frazer
Justin Frazer

Reputation: 35

Objective-C MPMediaItem with null URL

I have a project that makes use of a MPMediaPickerController to select audio files from the Media Player. However, when trying to store it's URL, I am getting nothing but null returned. My code can be seen below:

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

For whatever reason, my *assetURL is nil upon selecting a single audio item from the device's library. The only answers i could find with respect to this problem often relate to the url being null when a cloud item is selected from the MediaPicker. However, as seen above, I have set showCloudItems = NO.

I would greatly appreciate any help; let me know if you need any additional information!

Upvotes: 0

Views: 626

Answers (1)

Justin Frazer
Justin Frazer

Reputation: 35

Turns out, music from the "iCloud Music Library" is DRM protected and therefore, the assetURL is left null when selected in the MPMediaPickerController. The simple fix was to turn off the "iCloud Music Library" setting from within the settings for the "Music" app. I would expect picker.showsCloudItems = NO; to prevent this, but apparently I was mistaken.

Upvotes: 1

Related Questions