0

I'm using this code for getting the Artwork, but it's not workout for me. What's the wrong in this code.Suggest me. Thanks.

MPMediaQuery *mySongsQuery = [MPMediaQuery songsQuery];
            NSArray *SongsList = [mySongsQuery collections];
            for (MPMediaItemCollection *SongsArt in SongsList) {
NSArray *songs = [SongsArt items];
                for (MPMediaItem *song in songs) {

                    if ([(MPMediaItem*)item valueForProperty:MPMediaItemPropertyAssetURL] != nil) {
                    CGSize artworkImageViewSize = CGSizeMake(40, 40);
                    MPMediaItemArtwork *artwork = [song valueForProperty:MPMediaItemPropertyArtwork];
                    UIImage * image = [artwork imageWithSize:artworkImageViewSize];
                    if (image!= nil)
                    {
                        imgv_songImageView.image = image;
                    }
                    else
                    {
                        imgv_songImageView.image = [UIImage imageNamed:@"musicD-jpeg.png"];
                    }
       }
    }
Ravikumar
  • 85
  • 1
  • 16

2 Answers2

0

I assume you just want to loop through all songs in the music library so I don't see a need for collections:

MPMediaQuery *mySongsQuery = [MPMediaQuery songsQuery];

for (MPMediaItem *item in mySongsQuery.items) {
  if (![[item valueForProperty:MPMediaItemPropertyIsCloudItem]boolValue]) {

    CGSize artworkImageViewSize = CGSizeMake(40, 40);
    MPMediaItemArtwork *artwork = [song valueForProperty:MPMediaItemPropertyArtwork];
    UIImage *image = [artwork imageWithSize:artworkImageViewSize];

    if (image) {
      imgv_songImageView.image = image;
    } else {
      imgv_songImageView.image = [UIImage imageNamed:@"musicD-jpeg.png"];
    }
  }
}

I'm not sure why you want to check for the Asset URL but I've left it in.

sooper
  • 5,991
  • 6
  • 40
  • 65
0

Here i am posting the code to get the tracks and sorting them alphabetically. Its written in swift3.

    /// Get all the songs in the device and display in the tableview
///
func getAllSongs() {
    let query: MPMediaQuery = MPMediaQuery.songs()
    let allSongs = query.items

    allSongItems?.removeAll()

    guard allSongs != nil else {
        return
    }

    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
            allSongItems?.append(trackInfo)
            index += 1
        }
    }

    // Sort the songs alphabetically
    let sortedArray: [SongItem]? = allSongItems?.sorted {
        $0.songName!.localizedCompare($1.songName!) == .orderedAscending
    }
    allSongItems?.removeAll()
    if let arr = sortedArray {
        allSongItems?.append(contentsOf: arr)
    }


}
Abdul Yasin
  • 3,480
  • 1
  • 28
  • 42