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?
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
}