如何在ios中获取每张专辑的歌曲和专辑插图,歌曲列表和播放次数

时间:2014-08-01 10:33:03

标签: objective-c ios7

我需要实现此功能,请帮助我。

第一个视图控制器

我正在使用此代码获取相册的艺术作品。这工作正常,但我想要显示歌曲列表和播放次数。当我选择这里的专辑意味着[1st viewcontroller],我们去第二个viewcontroller和显示歌曲列表,如果我们选择将播放的歌曲。 感谢。

MPMediaQuery *query = [[MPMediaQuery alloc] init];
[query setGroupingType: MPMediaGroupingAlbum];

NSArray *albums = [query collections];
NSMutableArray *albumsWithArt = [NSMutableArray arrayWithCapacity:[albums count]];
for (id album in albums) {
    MPMediaItemArtwork *artwork = [[album representativeItem] valueForProperty:MPMediaItemPropertyArtwork];
    if (artwork) {
        [albumsWithArt addObject:album];
    }
}
self.albums = [NSArray arrayWithArray:albumsWithArt];

2 个答案:

答案 0 :(得分:1)

我得到了解决方案。现在我收到每张专辑歌曲,我选择了专辑。 可能这个解决方案很有用,谁具有相同的功能。

MPMediaQuery *albumsQuery = [MPMediaQuery albumsQuery];
        NSArray *albums = [albumsQuery collections];
        NSArray * al = [NSArray arrayWithObjects:[albums objectAtIndex:indexPath.row], nil];
        MPMediaItemCollection *albumCollection;
        for (albumCollection in al) {
            NSString *selectedAlbumTitle = [[albumCollection representativeItem] valueForProperty:MPMediaItemPropertyAlbumTitle];
            NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
            [dic setObject:selectedAlbumTitle forKey:@"AlbumName"];
            NSArray *songs = albumCollection.items;
            NSLog(@"album title is %@",selectedAlbumTitle);
            NSMutableArray * songsArray = [[NSMutableArray alloc]init];
            for (MPMediaItem *song in songs) {
                NSLog(@"the album songs title is--->%@",[song valueForProperty: MPMediaItemPropertyTitle]);
                [songsArray addObject:[song valueForProperty: MPMediaItemPropertyTitle]];
            }
            [dic setObject:songsArray forKey:@"Song"];
            [localSongsList addObject:dic];
            NSLog(@"alub song list %@",localSongsList);
        }

答案 1 :(得分:0)

为每个相册分别使用数组并分配内存,以便为每个新相册刷新数组。

NSArray * albumsArray = [NSArray arrayWithObjects:[albums
    objectAtIndex:indexPath.row], nil];
                MPMediaItemCollection *albumCollection;
                for (albumCollection in albumsArray) {
                    NSString *selectedAlbumTitle = [[albumCollection representativeItem] valueForProperty:MPMediaItemPropertyAlbumTitle];
                    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
                    [dic setObject:selectedAlbumTitle forKey:@"AlbumName"];
                    NSArray *songs = albumCollection.items;
                    NSLog(@"album title is %@",selectedAlbumTitle);
                    NSMutableArray * songsArray = [[NSMutableArray alloc]init];
                    for (MPMediaItem *song in songs) {
                        NSLog(@"the album songs title is--->%@",[song valueForProperty: MPMediaItemPropertyTitle]);
                        [songsArray addObject:[song valueForProperty: MPMediaItemPropertyTitle]];
                    }
                    [dic setObject:songsArray forKey:@"Song"];
                    [localSongsList addObject:dic];
                    NSLog(@"alub song list %@",localSongsList);
                }