18

I would like to know if an MPMediaItem that represents a music track is for a Fairplay/DRM-protected item. Any way to do this?

7 Answers 7

10

Here's how I do it:

MPMediaItem* item;

NSURL* assetURL = [item valueForProperty:MPMediaItemPropertyAssetURL];
NSString *title=[item valueForProperty:MPMediaItemPropertyTitle];

if (!assetURL) {
    /*
     * !!!: When MPMediaItemPropertyAssetURL is nil, it typically means the file
     * in question is protected by DRM. (old m4p files)
     */
    NSLog(@"%@ has DRM",title);
}
5
  • 1
    Hi Guys, Could you please suggest how can we play these DRM protected files using AVPlayer? Feb 28, 2012 at 6:02
  • 1
    AVPlayer cannot play old DRM. You need to get the user to upgrade the audio files with iTunes Plus to convert the tracks to open DRM which is what all songs are sold as now.
    – Brennan
    Mar 16, 2012 at 17:15
  • I checked a few songs I bought from iTunes in the last year and they exhibit the same problem -- MPMediaItemPropertyAssetURL returns (null). The files are shown in iTunes as "Purchased AAC audio file" and have an extension of .m4a. So it seems like this problem still exists, or went away and came back again since the comments above...?
    – arlomedia
    Jan 24, 2014 at 2:00
  • To your comment on DRM, I've also seen MPMediaItemPropertyAssetURL return nil for tracks that were not completely downloaded from iTunes/iCloud. Fixed it by forcing the download in the Apple Music app. Mar 29, 2014 at 18:14
  • 1
    This is not the correct way to check because MPMediaItemPropertyAssetURL is not nil on iOS 11 at least for songs saved offline via Apple Music but they are definitely DRM protected and AVPlayer too is unable to play them. Please check my answer below for details on this. Dec 7, 2017 at 11:57
6

Since iOS 4.2 there is a way. There may be a more effective way then the example here (but for my app I needed to create AVPlayerItems anyway).

MPMediaItem item;
NSURL *assetURL = [item valueForProperty:MPMediaItemPropertyAssetURL];
AVPlayerItem *avItem = [[AVPlayerItem alloc] initWithURL:assetURL];
BOOL fairplayed = avItem.asset.hasProtectedContent;
5
  • 2
    Since [item valueForProperty:MPMediaItemPropertyAssetURL] returns nil for a DRM, your AVPlayerItem will be created with nil ?
    – ıɾuǝʞ
    Dec 12, 2011 at 9:45
  • Possibly. Is it a safe assumption that the asset url returned is always nil for protected content?
    – voidStern
    Dec 19, 2011 at 10:35
  • I got the issue by not checking if assetURL is nil, an with a device with iOS > 4.2 (4.3.4 exactly)
    – ıɾuǝʞ
    Dec 20, 2011 at 16:50
  • 5
    Unprotected items which are not currently playable will also return nil. Most commonly items in iTunes Match which are not local to the device.
    – TurqMage
    Jan 4, 2012 at 0:38
  • 1
    This is the correct way to check. Also, MPMediaItemPropertyAssetURL is not nil on iOS 11 at least for songs saved offline via Apple Music but AVPlayer is unable to play them since they are DRM protected. Check my answer below for details on this. Dec 7, 2017 at 11:54
5

From iOS 4.2 the AVAsset class has a property hasProtectedContent so you can check:

NSURL *assetURL = [item valueForProperty:MPMediaItemPropertyAssetURL];
AVAsset *asset = [AVAsset assetWithURL:assetURL];

if ([asset hasProtectedContent] == NO) {..do your stuff..}
2
  • 3
    Note that this hasProtectedContent call is relatively slowing; calling it on about 100 non-DRMed songs was taking around 4 seconds on an iphone 4S.
    – JosephH
    Aug 1, 2012 at 15:11
  • Of course but that is only way to make it work. Adding that code in background queue solves the problem and you can display progress bar so user can know that operation will last a little bit longer that other operations.
    – Josip B.
    Aug 13, 2012 at 18:18
4

MPMediaItemPropertyAssetURL is not nil on iPhone X running iOS 11 for songs saved offline via Apple Music but AVPlayer is unable to play them since they are DRM protected. The same song returns MPMediaItemPropertyAssetURL nil on iOS 9.

MPMediaItemPropertyAssetURL returns nil for songs added to Library via Apple Music but not available offline - both on iOS 9 & 11.

Thus, @voidStern's answer (and not Justin Kent's) is the correct way to test for DRM-protected asset.

Swift 4 version of voidStern's answer (works perfectly for me on iOS 9 to 11):

let itemUrl = targetMPMediaItem?.value(forProperty: MPMediaItemPropertyAssetURL) as? URL
if itemUrl != nil {
    let theAsset = AVAsset(url: itemUrl!)
    if theAsset.hasProtectedContent {
        //Asset is protected
        //Must be played only via MPPlayer
    } else {
        //Asset is not protected
        //Can be played both via AVPlayer & MPPlayer\
    }
} else {
    //probably the asset is not avilable offline
    //Must be played only via MPPlayer
}

Another correct way of checking for DRM-protected asset is by making use of protectedAsset property of MPMediaItem - an answer by @weirdyu. But, this property is available only on iOS 9.2 and above.

Swift 4 solution for this method (works perfectly for me on iOS 9.2 and above):

if #available(iOS 9.2, *) {
    if (targetMPMediaItem?.hasProtectedAsset)! {
        //asset is protected
        //Must be played only via MPMusicPlayer
    } else {
        //asset is not protected
        //Can be played both via AVPlayer & MPMusicPlayer
    }
} else {
    //Fallback on earlier versions
    //Probably use the method explained earlier
}
1

iOS9.2+: Please use MPMediaItem "protectedAsset" property

iOS9.2-: Judge MPMediaItem"assetURL"property is nil or not

2
  • this property is available only on iOS 9.2 and above Dec 7, 2017 at 11:49
  • @Mohit Singh Thanks for your comment, I have add something.
    – weirdyu
    Jan 20, 2018 at 12:43
0

Justin Kents' solution works great. I recommend using blocks though or else the UX will suffer if you deal with a bunch of songs:

-(void)checkDRMprotectionForItem:(MPMediaItem*)item OnCompletion:(void (^)(BOOL drmProtected))completionBlock
{
    dispatch_async(_drmCheckQueue, ^{
        BOOL drmStatus;
        NSURL* assetURL = [item valueForProperty:MPMediaItemPropertyAssetURL];
        if (!assetURL) {
            drmStatus = YES;
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            if (completionBlock) {
                completionBlock(drmStatus);
            }
    });
    });
}
0

Now I'm building on swift 2 for ios 9, I found my code broken using hasProtectedContent or using nil url test. I've found the following code work:

    let playerItem = AVPlayerItem(URL: mpMediaItem.assetURL!)
    playableByAVPlayer = (playerItem.status == .ReadyToPlay) ? true : false

If the item is not playable by AV Player, then it's a DRM item and should be played by iPod Player (now called SystemMusicPlayer).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.