Pergunta

I'm setting N items in an MPMediaItemsCollection, where some items aren't unique (the collection represents a playlist, where the same song might appear twice).

- (void)setLastSongWithItemCollection:(MPMediaItemCollection *)itemCollection
{
    [_musicPlayer setQueueWithItemCollection: itemCollection];
    NSLog(@"itemCollection count %u", itemCollection.count);
    NSLog(@"itemCollection lastObject index: %u", [itemCollection.items indexOfObject:itemCollection.items.lastObject]);
    _musicPlayer.nowPlayingItem = [[itemCollection items] lastObject];

}

In one example, I'm generating itemCollection with 4 songs, where the last song is the first one repeated. If I attempt to get the last object in the list, _musicPlayer will always play the first item.

The first NSLog prints "itemCollection count 4" clearly indicating there are four items in the items array. However, the second NSLog prints "itemCollection lastObject index:0", indicating that items.lastObject does not return the object and the last index, but rather the first one where that same media item occurs.

Can anyone shed some light on this?

CORRECTION: As pointed out, indexOfObject: is not a valid test for returning the index of a repeated object

Still, _musicPlayer.nowPlayingItem = [[itemCollection items] lastObject] sets the first instance identical to lastObject to the media player. It seems the nowPlayingItem setter calling indexOfObject: on itemCollection. Workarounds?

Follow-up

It seems there is no way to select an index in the queue of an MPMediaPlayer, and setNowPlayingItem: calls indexOfObject: To find a selected media item. The result is that indexOfNowPlayingItem will return the index of the first instance identical to nowPlayingItem in the queue. In my case, this affects the visual representation of the queue that I have in my app (it's a scrollview with a panel for each song). It's possible that MPMediaItemCollection was not intended for use with playlists where MPMediaItems can occur more than once. I will fill a bug report in the interest of getting more information on the subject.

Foi útil?

Solução

This really has nothing to do with MPMediaItemsCollection specifically. Rather, it's just the behavior of NSArray.

As the documentation for -[NSArray indexOfObject:] states, it "returns the lowest index whose corresponding array value is equal to a given object." In this case, equality is determined by sending each object an -isEqual: message.

So, if you have an array:

NSArray *array = @[@"A", @"B", @"C", @"A"];

You'll see the following:

[array indexOfObject:@"A"] // returns 0
[array indexOfObject:array[3]] // also returns 0
[array indexOfObject:[array lastObject]] // also returns 0

Essentially, -[NSArray indexOfObject] looks like this:

- (void)indexOfObject:(id)object
{
    for (NSUInteger i=0; i<[self count]; i++) {
        if ([self[i] isEqual:object]) return i;
    }
    return NSNotFound;
}

If you want to know the indexes for all matching objects in the array, use -[NSArray indexesOfObjectsPassingTest:]

NSArray *array = @[@"A", @"B", @"C", @"A"];
NSIndexSet *indexes = [array indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    return [obj isEqual:@"A"];
}];
NSLog(@"Indexes: %@", indexes);

>>Indexes: <NSIndexSet: 0x7fbc9b40b170>[number of indexes: 2 (in 2 ranges), indexes: (0 3)]

If you really need to know which index should be associated with your use of a non-unique item in the playlist, you'll have to keep track of that yourself by storing it when you retrieve the media item in the first place.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top