Pergunta

I'm trying to play songs from an iPod playlist. As far as playlist may have videos too, I want to play only songs in them.

    MPMusicPlayerController *mp = [MPMusicPlayerController applicationMusicPlayer];

    MPMediaQuery *query = [MPMediaQuery songsQuery];

    query.groupingType = MPMediaGroupingPlaylist;

    NSArray *playlists = [query collections];

    BOOL found = NO;

    for(MPMediaPlaylist *playlist in playlists)
    {
        NSString *name = [playlist valueForProperty:MPMediaPlaylistPropertyName];
        if([name caseInsensitiveCompare:@"MyPlaylist"] == NSOrderedSame && [playlist count] > 0)
        {
            MPMediaItemCollection *collection = [MPMediaItemCollection collectionWithItems:playlist.items];
            [mp setQueueWithItemCollection:collection];
            found = YES;
            NSLog(@"items count %d", [playlist count]);
            for (MPMediaItem *item in [collection items]){
                NSLog(@"%@", [item valueForKey:MPMediaItemPropertyMediaType]);
            }
            break;
        }
    }

After I load a playlist with 1 song a 2 videos, the output is:

items count 3
256
256
1

I don't understand why I'm getting videos too, if the query is intended to retrieve songs, and media items have media type MPMediaTypeMovie (256 or 1 << 8).

Any suggestion?

Foi útil?

Solução

One possible workaround would be to create a custom query using predicates. This can be implemented by using the MPMediaQuery items array rather than the collections array. The collections array is an array of MPMediaItemCollections. The items array is an array of MPMediaItems, so you can filter the query by MPMediaItemPropertyMediaType. MPMediaItemPropertyMediaType is only applicable to a MPMediaItem and not a MPMediaItemCollection.

Initialize the query with a set of predicates based on the playlist title and media type. This will select your playlist and filter out any videos from the query:

NSString *playlistName = @"MyPlaylist";
MPMediaPropertyPredicate *playlistPredicate = [MPMediaPropertyPredicate predicateWithValue:playlistName
                                                                               forProperty:MPMediaPlaylistPropertyName];

NSNumber *mediaTypeNumber = [NSNumber numberWithInteger:MPMediaTypeMusic]; // == 1
MPMediaPropertyPredicate *mediaTypePredicate = [MPMediaPropertyPredicate predicateWithValue:mediaTypeNumber
                                                                                forProperty:MPMediaItemPropertyMediaType];

NSSet *predicateSet = [NSSet setWithObjects:playlistPredicate, mediaTypePredicate, nil];
MPMediaQuery *mediaTypeQuery = [[MPMediaQuery alloc] initWithFilterPredicates:predicateSet];
[mediaTypeQuery setGroupingType:MPMediaGroupingPlaylist];

These MPMediaPropertyPredicates will NOT filter if you return your array from the MPMediaQuery collections array. You should see media item types other than 1 in the log statements:

NSArray *playlistCollections = [mediaTypeQuery collections];

[playlistCollections enumerateObjectsUsingBlock:^(MPMediaPlaylist *playlist, NSUInteger idx, BOOL *stop) {
    NSLog (@"Playlist title: %@", [playlist valueForProperty:MPMediaPlaylistPropertyName]);
    NSArray *songs = [playlist items];
    [songs enumerateObjectsUsingBlock:^(MPMediaItem *song, NSUInteger idx, BOOL *stop) {
        NSString *songTitle = [song valueForProperty:MPMediaItemPropertyTitle];
        NSInteger mediaValue = [[song valueForProperty:MPMediaItemPropertyMediaType] integerValue];
        if (mediaValue != 1) 
            NSLog(@"title: %@ - media type value: %d", songTitle, mediaValue);
    }]; // Should return other media types here.
}];  

The MPMediaPropertyPredicates WILL filter if you return your array from MPMediaQuery items:

NSArray *playlistItems = [mediaTypeQuery items];

[playlistItems enumerateObjectsUsingBlock:^(MPMediaItem *song, NSUInteger idx, BOOL *stop) {
    NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
    NSInteger mediaValue = [[song valueForProperty:MPMediaItemPropertyMediaType] integerValue];
    if (mediaValue != 1) 
        NSLog(@"title: %@ - media type value: %d", songTitle, mediaValue);
}]; // Should return nothing here.

You could also apply a media type filter to your entire library by using only the mediaTypePredicate on the songsQuery:

MPMediaQuery *query = [MPMediaQuery songsQuery];
[query setGroupingType:MPMediaGroupingPlaylist];
[mediaTypeQuery addFilterPredicate:mediaTypePredicate];
NSArray *playlists = [query items];
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top