11

I have been using the following code to extract the asset url from the MPMediaItem object returned from the MPMediaItemPickerController so that I can copy music files from a users iPhone itunes music library to the documents folder for processing, but on iPhone 5s I always get a null value from the MPMediaItemPropertyAssetURL, but when I run the same code on iPhone 4 or iPhone 5 it works as it should returning a proper url.

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

    [self dismissViewControllerAnimated:YES completion:nil];

        if(mediaItemCollection){
           MPMediaItem *mediaItem = (MPMediaItem *)[mediaItemCollection.items objectAtIndex: 0];
           NSString *songTitle = [mediaItem valueForProperty: MPMediaItemPropertyTitle];
           NSLog(@"songtitle: %@", songTitle);
           NSURL *assetURL = [mediaItem valueForProperty: MPMediaItemPropertyAssetURL];
           NSLog(@"%@", assetURL);
        }

}

I have tried removing arm64 from valid architectures and only building for armv7 and armv7s, but that didn't fix this problem.

Does anyone know why this is happening and how I can fix it or if there is a workaround I can use? I need to be able to copy music from the iPhone's music library to the documents folder so that I can process the music properly for a dj application.

Thanks

Greg Ellis
  • 1,957
  • 3
  • 19
  • 27
  • 1
    it may help you http://stackoverflow.com/questions/5571036/how-to-detect-if-an-mpmediaitem-represents-a-drm-protected-audio-track-on-ios/6401317#6401317 – Shamsudheen TK Jan 17 '14 at 21:34
  • 1
    unfortunately that is precisely the code I am already using which is working on all of my devices except for the iPhone 5s. The following code always results in a NULL assetURL variable on iPhone 5s. NSURL *assetURL = [item valueForProperty:MPMediaItemPropertyAssetURL]; I guess it must have something to do with the 64 bit architecture, but I can't imagine why it would be a problem... Thanks – Greg Ellis Jan 17 '14 at 23:42
  • I'm having this problem too, but with a iPhone 5. MPMediaItemPropertyAssetURL is always returning nil for a track that was returned from a query on a device - so it must be there (and I can play it from iPod) – Jonny May 09 '14 at 08:57
  • 7
    Hey Jonny, I found out that the song was actually *not* on my device. It was listed in the media library, but was actually still in iCloud. Once I downloaded the song to my device then the problem was solved. I hope this can help you too. – Greg Ellis May 31 '14 at 21:00
  • @GregEllis You should add your comment as the answer, this totally solved it for me :) It would be nice to know if there is a way to stream the data from iCloud without the URL – Jeroen Bouma Nov 21 '14 at 16:53

2 Answers2

26

I found out that the problem was the song I was trying to get the MPMediaItemPropertyAssetURL property for was actually not on my device. It was listed in the media library, but was actually still in iCloud. Once I downloaded the song to my device then the problem was solved. As much as I don't like answering my own question I took Jeroen's advice so that it can hopefully help others.

Greg Ellis
  • 1,957
  • 3
  • 19
  • 27
  • 2
    @Greg Ellis I bought 3 albums from iTunes directly, and downloaded on my iPhone. And for some songs I am getting NIL value for URL. So still this is strange. – Bonnke Jul 13 '15 at 05:06
  • If the song is protected asset, it also will return a nil value. You can check it by [mediaItem hasProtectedAsset] – Nadeesha Lakmal Oct 02 '19 at 10:51
1

We can add filter that does not show iCloud items with

[mediaPicker setShowsCloudItems:NO];
Muhammed Tanriverdi
  • 3,230
  • 1
  • 23
  • 24