为什么MPMediaItemPropertyAssetURL返回nil

问题描述 投票:1回答:1

为什么音乐nil的网址?

MPMediaPlaylist *playlist = [self.musicCollection objectAtIndex:cindex];
MPMediaItem *item = [playlist.items objectAtIndex:mindex];
NSUrl *url = [item valueForProperty:MPMediaItemPropertyAssetURL] ;
NSLog(@"URL == %@");

为什么要记录url = nil

objective-c avaudioplayer
1个回答
0
投票

由于两个原因,MPMediaItemPropertyAssetURL / MPMediaItem assetURL为null / nil。

  1. 音乐不会下载到您的设备,而只会添加到音乐库中。
  2. 音乐已加载但受DRM保护。

受DRM保护的资产无法使用AVPlayer播放,它只能使用MPMusicPlayer播放。因此,在继续使用AVPlayer之前,您必须先检查两件事。

  1. MPMediaItemPropertyAssetURL是零?
  2. MPMediaItem受到保护?

请看下面的代码......

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(@"Its DRM protected");
    }
© www.soinside.com 2019 - 2024. All rights reserved.