Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9226375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:48:54+00:00 2026-06-18T04:48:54+00:00

I’m setting N items in an MPMediaItemsCollection, where some items aren’t unique (the collection

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:48:56+00:00Added an answer on June 18, 2026 at 4:48 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.