ios - 从 iOS 媒体库中获取所有艺术家的列表

标签 ios mpmusicplayercontroller mpmediaitemcollection

我希望获取用户库中所有艺术家的 MPMediaItemCollectionNSArray。这是我当前的代码(显然不起作用):

- (void)viewDidLoad
{
    [super viewDidLoad];
    MPMediaQuery *artistsQuery = [MPMediaQuery artistsQuery];
    self.artistsArray = artistsQuery.collections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.artistsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ArtistsCell"];
    cell.textLabel.text = [(MPMediaItemCollection *)self.artistsArray[indexPath.row]   valueForProperty:MPMediaPlaylistPropertyName];
    NSLog(@"%@", cell.textLabel.text);
    return cell;
}

最佳答案

对于艺术家列表,请使用 MPMediaItemPropertyArtist 而不是 MPMediaPlaylistPropertyName 作为媒体项属性键。

由于您的数据源使用的是 MPMediaItemCollection,因此请使用 representativeItem 属性获取每个集合的艺术家标题字符串。

然后将 textLabel 的文本设置为艺术家字符串。

MPMediaItemCollection *artistCollection = self.artistsArray[indexPath.row];
NSString *artistTitle = [[artistCollection representativeItem] valueForProperty:MPMediaItemPropertyArtist];
cell.textLabel.text = artistTitle;

关于ios - 从 iOS 媒体库中获取所有艺术家的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14548301/

相关文章:

objective-c - 如何使 View Controller 可滚动?

ios - 检测所有按钮的单一控制事件

ios - 通过用户库中的耳机播放音乐

iphone - 艺术家专辑数量

每次打开应用程序时,ios都会加载第一页

ios - 在 Swift 3 中动态调整 UILabel 的宽度

ios - 如何同时录制视频和播放音频(swift教程)

ios - 如何在 iOS 应用程序中播放 iTunes 广播?

ios - 如何在 ios 应用程序的 MPMusicPlayerController 中获取当前播放的歌曲数据

ios - MPMediaQuery 无效值