如何检测 MPMediaItem 是否代表 iOS 上受 DRM 保护的音轨

发布于 2024-10-30 23:39:08 字数 67 浏览 1 评论 0原文

我想知道代表音乐曲目的 MPMediaItem 是否适用于受 Fairplay/DRM 保护的项目。有办法做到这一点吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

薄荷梦 2024-11-06 23:39:08

我是这样做的:

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);
}

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);
}
岁吢 2024-11-06 23:39:08

从 iOS 4.2 开始就有了一种方法。可能有比这里的示例更有效的方法(但对于我的应用程序,我无论如何都需要创建 AVPlayerItems)。

MPMediaItem item;
NSURL *assetURL = [item valueForProperty:MPMediaItemPropertyAssetURL];
AVPlayerItem *avItem = [[AVPlayerItem alloc] initWithURL:assetURL];
BOOL fairplayed = avItem.asset.hasProtectedContent;

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;
玩物 2024-11-06 23:39:08

从 iOS 4.2 开始,AVAsset 类有一个 hasProtectedContent 属性,因此您可以检查:

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

if ([asset hasProtectedContent] == NO) {..do your stuff..}

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..}
懷念過去 2024-11-06 23:39:08

对于通过 Apple Music 离线保存的歌曲,在运行 iOS 11 的 iPhone X 上,MPMediaItemPropertyAssetURL非零 em> 但 AVPlayer 无法播放它们,因为它们受 DRM 保护。同一首歌曲在 iOS 9 上返回 MPMediaItemPropertyAssetURL nil

对于通过 Apple Music 添加到库中的歌曲,MPMediaItemPropertyAssetURL 返回 nil,但无法离线使用 - 均在 iOS 上9& 11。

因此,@voidStern 的答案(而不是 Justin Kent 的答案)是测试受 DRM 保护的资产的正确方法。

voidStern 答案的 Swift 4 版本(在 iOS 9 到 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
}

检查受 DRM 保护的资产的另一种正确方法是使用 MPMediaItem 的 protectedAsset 属性 - @weirdyu 的回答。但是,此属性仅在 iOS 9.2 及更高版本上可用。

此方法的 Swift 4 解决方案(在 iOS 9.2 及更高版本上非常适合我):

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
}

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
}
蝶舞 2024-11-06 23:39:08

iOS9.2+:
iOS9.2-请使用MPMediaItem“protectedAsset”属性


判断MPMediaItem"assetURL"属性是否为nil

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

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

抠脚大汉 2024-11-06 23:39:08

贾斯汀·肯特的解决方案效果很好。我建议使用块,否则如果你处理一堆歌曲,用户体验将会受到影响:

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

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);
            }
    });
    });
}
孤独岁月 2024-11-06 23:39:08

现在我正在 ios 9 的 swift 2 上构建,我发现使用 hasProtectedContent 或使用 nil url 测试我的代码被破坏了。我发现以下代码有效:

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

如果该项目无法通过 AV 播放器播放,那么它是一个 DRM 项目,应该由 iPod 播放器(现在称为 SystemMusicPlayer)播放。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文