I am trying to build a media browser on iOS in objective-c. So far I can get the songsQuery:
_query = [MPMediaQuery songsQuery];
And in my tableView datasource I can get the number of sections and the section titles like this:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if(_query)
{
return _query.itemSections.count;
}
return 1;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return _query.itemSections[section].title;
}
That gives me (say) 26 sections, with titles like "A", "B", "C" and so on... as expected. What I don't understand is how to get the count of songs in each section. Ie how to get all the songs for sections "A" or "B" etc
If I understand your question correctly, you want to get the number of songs in each MPMediaQuery section.
From the documentation, MPMediaQuerySection
should include a range
property which stores the range of the array of items included in the section. The number of songs may then be obtained by taking the length
of the range. Like so:
NSUInteger numSongs = _query.itemSections[section].range.length;
You should also be able to obtain an array of all the songs in the section like this:
//The range of the song in the section
NSRange *sectionRange = _query.itemSections[section].range;
//Array of MPMediaItems in the section
NSArray *songsInSection = [_query.items subarrayWithRange: sectionRange];