Table of contents
Share Post
Or how to convert an MPMediaItem into an NSDictionary ?
That’s pretty easy :
MPMediaItem contains values targetable through the following properties :
MPMediaItemPropertyAlbumArtist MPMediaItemPropertyAlbumArtistPersistentID MPMediaItemPropertyAlbumPersistentID MPMediaItemPropertyAlbumTitle MPMediaItemPropertyAlbumTrackCount MPMediaItemPropertyAlbumTrackNumber MPMediaItemPropertyArtist MPMediaItemPropertyArtistPersistentID MPMediaItemPropertyArtwork MPMediaItemPropertyAssetURL MPMediaItemPropertyBeatsPerMinute MPMediaItemPropertyComments MPMediaItemPropertyComposer MPMediaItemPropertyComposerPersistentID MPMediaItemPropertyDiscCount MPMediaItemPropertyDiscNumber MPMediaItemPropertyGenre MPMediaItemPropertyGenrePersistentID MPMediaItemPropertyIsCloudItem MPMediaItemPropertyIsCompilation MPMediaItemPropertyLyrics MPMediaItemPropertyMediaType MPMediaItemPropertyPersistentID MPMediaItemPropertyPlaybackDuration MPMediaItemPropertyPodcastPersistentID MPMediaItemPropertyReleaseDate
So you can easily write the code to convert everything, taking care of not including nil values :
- (NSDictionary *)fastEnumarateMPMedia:(MPMediaItem *)media { NSMutableDictionary *dico =[[NSMutableDictionary alloc] init]; if ([media valueForProperty: MPMediaItemPropertyAlbumArtist]) [dico setObject:[media valueForProperty: MPMediaItemPropertyAlbumArtist] forKey:@"MPMediaItemPropertyAlbumArtist"]; if ([media valueForProperty: MPMediaItemPropertyAlbumArtistPersistentID]) [dico setObject:[media valueForProperty: MPMediaItemPropertyAlbumArtistPersistentID] forKey:@"MPMediaItemPropertyAlbumArtistPersistentID"]; if ([media valueForProperty: MPMediaItemPropertyAlbumPersistentID]) [dico setObject:[media valueForProperty: MPMediaItemPropertyAlbumPersistentID] forKey:@"MPMediaItemPropertyAlbumPersistentID"]; if ([media valueForProperty: MPMediaItemPropertyAlbumTitle]) [dico setObject:[media valueForProperty: MPMediaItemPropertyAlbumTitle] forKey:@"MPMediaItemPropertyAlbumTitle"]; if ([media valueForProperty: MPMediaItemPropertyAlbumTrackCount]) [dico setObject:[media valueForProperty: MPMediaItemPropertyAlbumTrackCount] forKey:@"MPMediaItemPropertyAlbumTrackCount"]; if ([media valueForProperty: MPMediaItemPropertyAlbumTrackNumber]) [dico setObject:[media valueForProperty: MPMediaItemPropertyAlbumTrackNumber] forKey:@"MPMediaItemPropertyAlbumTrackNumber"]; if ([media valueForProperty: MPMediaItemPropertyArtist]) [dico setObject:[media valueForProperty: MPMediaItemPropertyArtist] forKey:@"MPMediaItemPropertyArtist"]; if ([media valueForProperty: MPMediaItemPropertyArtistPersistentID]) [dico setObject:[media valueForProperty: MPMediaItemPropertyArtistPersistentID] forKey:@"MPMediaItemPropertyArtistPersistentID"]; if ([media valueForProperty: MPMediaItemPropertyArtwork]) [dico setObject:[media valueForProperty: MPMediaItemPropertyArtwork] forKey:@"MPMediaItemPropertyArtwork"]; if ([media valueForProperty: MPMediaItemPropertyAssetURL]) [dico setObject:[media valueForProperty: MPMediaItemPropertyAssetURL] forKey:@"MPMediaItemPropertyAssetURL"]; if ([media valueForProperty: MPMediaItemPropertyBeatsPerMinute]) [dico setObject:[media valueForProperty: MPMediaItemPropertyBeatsPerMinute] forKey:@"MPMediaItemPropertyBeatsPerMinute"]; if ([media valueForProperty: MPMediaItemPropertyComments]) [dico setObject:[media valueForProperty: MPMediaItemPropertyComments] forKey:@"MPMediaItemPropertyComments"]; if ([media valueForProperty: MPMediaItemPropertyComposer]) [dico setObject:[media valueForProperty: MPMediaItemPropertyComposer] forKey:@"MPMediaItemPropertyComposer"]; if ([media valueForProperty: MPMediaItemPropertyComposerPersistentID]) [dico setObject:[media valueForProperty: MPMediaItemPropertyComposerPersistentID] forKey:@"MPMediaItemPropertyComposerPersistentID"]; if ([media valueForProperty: MPMediaItemPropertyDiscCount]) [dico setObject:[media valueForProperty: MPMediaItemPropertyDiscCount] forKey:@"MPMediaItemPropertyDiscCount"]; if ([media valueForProperty: MPMediaItemPropertyDiscNumber]) [dico setObject:[media valueForProperty: MPMediaItemPropertyDiscNumber] forKey:@"MPMediaItemPropertyDiscNumber"]; if ([media valueForProperty: MPMediaItemPropertyGenre]) [dico setObject:[media valueForProperty: MPMediaItemPropertyGenre] forKey:@"MPMediaItemPropertyGenre"]; if ([media valueForProperty: MPMediaItemPropertyGenrePersistentID]) [dico setObject:[media valueForProperty: MPMediaItemPropertyGenrePersistentID] forKey:@"MPMediaItemPropertyGenrePersistentID"]; if ([media valueForProperty: MPMediaItemPropertyIsCloudItem]) [dico setObject:[media valueForProperty: MPMediaItemPropertyIsCloudItem] forKey:@"MPMediaItemPropertyIsCloudItem"]; if ([media valueForProperty: MPMediaItemPropertyIsCompilation]) [dico setObject:[media valueForProperty: MPMediaItemPropertyIsCompilation] forKey:@"MPMediaItemPropertyIsCompilation"]; if ([media valueForProperty: MPMediaItemPropertyLyrics]) [dico setObject:[media valueForProperty: MPMediaItemPropertyLyrics] forKey:@"MPMediaItemPropertyLyrics"]; if ([media valueForProperty: MPMediaItemPropertyMediaType]) [dico setObject:[media valueForProperty: MPMediaItemPropertyMediaType] forKey:@"MPMediaItemPropertyMediaType"]; if ([media valueForProperty: MPMediaItemPropertyPersistentID]) [dico setObject:[media valueForProperty: MPMediaItemPropertyPersistentID] forKey:@"MPMediaItemPropertyPersistentID"]; if ([media valueForProperty: MPMediaItemPropertyPlaybackDuration]) [dico setObject:[media valueForProperty: MPMediaItemPropertyPlaybackDuration] forKey:@"MPMediaItemPropertyPlaybackDuration"]; if ([media valueForProperty: MPMediaItemPropertyPodcastPersistentID]) [dico setObject:[media valueForProperty: MPMediaItemPropertyPodcastPersistentID] forKey:@"MPMediaItemPropertyPodcastPersistentID"]; if ([media valueForProperty: MPMediaItemPropertyReleaseDate]) [dico setObject:[media valueForProperty: MPMediaItemPropertyReleaseDate] forKey:@"MPMediaItemPropertyReleaseDate"]; return dico; }