2010-10-15 12 views

Antwort

21

Verwenden Sie ein MPMediaQuery:

MPMediaQuery *allAlbumsQuery = [MPMediaQuery albumsQuery]; 
NSArray *allAlbumsArray = [allAlbumsQuery collections]; 

Die AllItems Array jetzt MPMediaItemCollections enthält, Gruppierung von Album gemacht wird. Jetzt können Sie durch die Arrays gehen.

for (MPMediaItemCollection *collection in allAlbumsArray) { 
    MPMediaItem *item = [collection representativeItem]; 
} 
4

Danke für die Antwort, hier arbeitet Beispielcode, der die Alben und Künstlern, falls jemand druckt es braucht:

NSMutableString *outText = [[NSMutableString alloc] initWithString:@"Albums:"]; 
[outText appendFormat:@"\r\n count:%i",[[[MPMediaQuery albumsQuery] collections] count]]; 
for (MPMediaItemCollection *collection in [[MPMediaQuery albumsQuery] collections]) { 
     [outText appendFormat:@"\r\n -%@",[[collection representativeItem] valueForProperty:MPMediaItemPropertyAlbumTitle]]; 
} 

[outText appendString:@"\r\n\r\n Artist:"]; 

for (MPMediaItemCollection *collection in [[MPMediaQuery artistsQuery] collections]) { 
     [outText appendFormat:@"\r\n -%@",[[collection representativeItem] valueForProperty:MPMediaItemPropertyArtist]]; 
} 
NSLog(@"%@",[outText autorelease]); 
0

Hier gehen Sie. Sie können die Alben und ihre Lieder bekommen.

/// Get all albums and their songs 
/// 
func getAllAlbums() { 
    let query: MPMediaQuery = MPMediaQuery.albums() 
    let allAlbums = query.collections 

    allAlbumItems?.removeAll() 

    guard allAlbums != nil else { 
     return 
    } 

    for collection in allAlbums! { 
     let item: MPMediaItem? = collection.representativeItem 

     let albumName = item?.value(forKey: MPMediaItemPropertyAlbumTitle) as? String ?? "<Unknown>" 
     let albumId = item!.value(forProperty: MPMediaItemPropertyAlbumPersistentID) as! NSNumber 
     let artistName = item?.value(forKey: MPMediaItemPropertyArtist) as? String ?? "<Unknown>" 

     let album = Album() 
     album.name = albumName 
     album.artistName = artistName 
     album.albumId = String(describing: albumId) 
     print("Album name: \(albumName)") 

     // Get all songs in this album 
     let mediaQuery = MPMediaQuery.songs() 
     let predicate = MPMediaPropertyPredicate.init(value: albumId, forProperty: MPMediaItemPropertyAlbumPersistentID) 
     mediaQuery.addFilterPredicate(predicate) 
     let song = mediaQuery.items 

     if let allSongs = song { 
      var index = 0 

      for item in allSongs { 
       let pathURL: URL? = item.value(forProperty: MPMediaItemPropertyAssetURL) as? URL 
       if pathURL == nil { 
        print("@Warning!!! Track : \(item) is not playable.") 
       } else { 
        let trackInfo = SongItem() 
        trackInfo.index = index 
        trackInfo.mediaItem = item 

        let title = item.value(forProperty: MPMediaItemPropertyTitle) as? String ?? "<Unknown>" 
        let artistName = item.value(forProperty: MPMediaItemPropertyArtist) as? String ?? "<Unknown>" 
        trackInfo.songName = title 
        trackInfo.artistName = artistName 

        trackInfo.isSelected = false 
        trackInfo.songURL = item.value(forProperty: MPMediaItemPropertyAssetURL) as? URL 
        album.songs?.append(trackInfo) 
        index += 1 
       } 
      } 

     } 

     // Finally add the album object to albums array 
     allAlbumItems?.append(album) 

    } 


    print("Total Album count: \(allAlbumItems?.count)") 

}