天天看点

iPod Library Access Programming Guide 4 - Using the iPod Library

Figure 4-1 illustrates how your application and the database access classes interact to retrieve media items.

iPod Library Access Programming Guide 4 - Using the iPod Library

Retrieving Ungrouped Media Items

Listing 4-1  Creating and using a generic media query which matches the entire contents of the library.
           
MPMediaQuery *everything = [[MPMediaQuery alloc] init]; 
NSLog(@"Logging items from a generic query...");
NSArray *itemsFromGenericQuery = [everything items];
for (MPMediaItem *song in itemsFromGenericQuery) {
    NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
    NSLog (@"%@", songTitle);
}
           
Listing 4-2  Constructing and applying a media property predicate
           
MPMediaPropertyPredicate *artistNamePredicate =
    [MPMediaPropertyPredicate predicateWithValue: @"Happy the Clown"
                                     forProperty: MPMediaItemPropertyArtist];
 
MPMediaQuery *myArtistQuery = [[MPMediaQuery alloc] init];
[myArtistQuery addFilterPredicate: artistNamePredicate];
 
NSArray *itemsFromArtistQuery = [myArtistQuery items];
           
Listing 4-3  Applying multiple predicates to an existing media query
           
MPMediaPropertyPredicate *artistNamePredicate =
    [MPMediaPropertyPredicate predicateWithValue: @"Sad the Joker"
                                     forProperty: MPMediaItemPropertyArtist];
 
MPMediaPropertyPredicate *albumNamePredicate =
    [MPMediaPropertyPredicate predicateWithValue: @"Stair Tumbling"
                                     forProperty: MPMediaItemPropertyAlbumTitle];
 
MPMediaQuery *myComplexQuery = [[MPMediaQuery alloc] init];
 
[myComplexQuery addFilterPredicate: artistNamePredicate];
[myComplexQuery addFilterPredicate: albumNamePredicate];
           
Listing 4-4  Applying multiple predicates when initializing a media query
NSSet *predicates =
    [NSSet setWithObjects: artistNamePredicate, albumNamePredicate, nil];
 
MPMediaQuery *specificQuery =
    [[MPMediaQuery alloc] initWithFilterPredicates: predicates];
           
Listing 4-5  Testing if a property key can be used for a media property predicate
if ([MPMediaItem canFilterByProperty: MPMediaItemPropertyGenre]) {
 
    MPMediaPropertyPredicate *rockPredicate =
        [MPMediaPropertyPredicate predicateWithValue: @"Rock"
                                         forProperty: MPMediaItemPropertyGenre];
 
    [query addFilterPredicate: rockPredicate];
}
           

Apple recommends that you always perform a check like this before constructing a media property predicate.

Retrieving Media Item Collections

Listing 4-6 shows how to retrieve all the songs by a particular artist, with those songs arranged into albums. The example logs the results to the Xcode debugger console.

Listing 4-6  Using grouping type to specify media item collections
MPMediaQuery *query = [[MPMediaQuery alloc] init];
 
[query addFilterPredicate: [MPMediaPropertyPredicate
                               predicateWithValue: @"Moribund the Squirrel"
                                      forProperty: MPMediaItemPropertyArtist]];
// Sets the grouping type for the media query
[query setGroupingType: MPMediaGroupingAlbum];
 
NSArray *albums = [query collections];
for (MPMediaItemCollection *album in albums) {
    MPMediaItem *representativeItem = [album representativeItem];
    NSString *artistName =
        [representativeItem valueForProperty: MPMediaItemPropertyArtist];
    NSString *albumName =
        [representativeItem valueForProperty: MPMediaItemPropertyAlbumTitle];
    NSLog (@"%@ by %@", albumName, artistName);
 
    NSArray *songs = [album items];
    for (MPMediaItem *song in songs) {
        NSString *songTitle =
            [song valueForProperty: MPMediaItemPropertyTitle];
        NSLog (@"\t\t%@", songTitle);
    }
}
           

You can see in Listing 4-6 that the value of the media query’s collections property is an array of arrays. The outer for loop iterates over the albums performed the specified artist. The inner for loop iterates over the songs in the current album.

The MPMediaQuery class includes several convenience constructors for creating queries that are preconfigured with a grouping type. The following statement, for example, attaches the “albums” grouping type to a newly-allocated query:

MPMediaQuery *query = [MPMediaQuery albumsQuery];
           

Using Media Item Artwork

One of the most useful and high-impact properties of a media item is its artwork. To display artwork, you use Interface Builder as well as Xcode. The steps are as follows:

  1. Add a UIImageView object to your view layout in Interface Builder.
  2. Add an IBOutlet instance variable to your view controller class to connect to the UIImageView object.
  3. Retrieve the artwork from the media item that owns it (having previously retrieved the media item as described in this chapter).
  4. Convert the artwork to a UIImage object, then assign it to your layout’s UIImageView object.

Listing 4-7 shows how to do steps 3 and 4.

Listing 4-7  Displaying album artwork for a media item
MPMediaItemArtwork *artwork =
    [mediaItem valueForProperty: MPMediaItemPropertyArtwork];
UIImage *artworkImage =
    [artwork imageWithSize: albumImageView.bounds.size];
 
if (artworkImage) {
    albumImageView.image = artworkImage;
} else {
    albumImageView.image = [UIImage imageNamed: @"noArtwork.png"];
}
           

继续阅读