4

I am working on a project where I need to play songs from iTunes Library in AVPlayer. For that, I am taking URL "ipod-library://item/item.mp3?id=1577682869916034242" of selected songs from iTunes Library and playing same in AVPlayer. Almost all songs get play, but for few songs MPMediaItemPropertyAssetURL returns nil URL. Also receive following error

-[AVAssetReader initWithAsset:error:] invalid parameter not satisfying: asset != ((void *)0)

Any suggestion on this? and why I am getting nil value from

MPMediaItemPropertyAssetURL

Also any idea how to stream or convert DRM Protected Media track into NSData?. Please advice.

Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
Sanchit Paurush
  • 6,114
  • 17
  • 68
  • 107
  • You can refer this http://stackoverflow.com/questions/5571036/how-to-detect-if-an-mpmediaitem-represents-a-drm-protected-audio-track-on-ios/6401317#6401317. I think you should check the url get from MPMediaItemPropertyAssetURL ```NSURL *assetURL = [item valueForProperty:MPMediaItemPropertyAssetURL]; if (assetURL && assetURL.aboluteString.length > 0) ...``` – Proton Jun 28 '16 at 08:11
  • @DungProton I can check this. But I want to know the reason, why it is nil. What if I want to play that song? – Sanchit Paurush Jun 28 '16 at 08:57
  • http://stackoverflow.com/questions/21195064/mpmediaitempropertyasseturl-returning-null-only-for-iphone-5s ? Is it because it's in the cloud? – Larme Jun 28 '16 at 09:08
  • It's DRM-protected media track (Digital rights management) ? You can check it by ``avItem.asset.hasProtectedContent`` – Proton Jun 28 '16 at 09:11
  • 1
    [mediaPicker setShowsCloudItems:NO]; try this – Jagveer Singh Jul 07 '16 at 10:06

3 Answers3

2

MPMediaItemPropertyAssetURL can returns null for two possible reason.

  1. The music is not downloaded to your device but added in music library only.
  2. The music is loaded but its DRM-protected.

DRM-protected asset is not possible to play using AVPlayer, its only able to play using MPMusicPlayer. So you must need to check two things before proceed with AVPlayer.

  1. MPMediaItemPropertyAssetURL is nil ?
  2. MPMediaItem is protected ?

Please see the code below….

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(@"DRM protected or not downloaded locally");
    }
emraz
  • 1,563
  • 20
  • 28
0

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.

Lal Krishna
  • 15,485
  • 6
  • 64
  • 84
0

Leaving this answer for other people like me.

Even though the music is on downloaded on the device, if it is DRM protected, MPMediaItem.value(forProperty: MPMediaItemPropertyAssetURL) will return nil as mentioned in the comment.

My app kept crashing and I confirmed this with my beta tester.

It seems like MPMusicPlayerController still supports playback so according to this answer.

funct7
  • 3,407
  • 2
  • 27
  • 33