MPMediaQuery to return local items only
Asked Answered
K

2

8

I want to display a UITableView with only the songs that are currently on the device.

If I query all items I (obviously) get all items, including the once that I purchased but not have downloaded. Here is the (part) of the code

@property (strong, nonatomic) NSMutableArray *songsList;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    MPMediaQuery *everything = [[MPMediaQuery alloc] init];
    NSArray *itemsFromGenericQuery = [everything items];
    self.songsList = [NSMutableArray arrayWithArray:itemsFromGenericQuery];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.songsList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"TableCellID";
    TableCellTitle *tablecell = (TableCellTitle *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
MPMediaItem *song = [self.songsList objectAtIndex:indexPath.row];

    NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
    tablecell.cellSongname.text = songTitle;

    return tablecell;
}

I have read a couple of things about involving

[song valueForProperty:MPMediaItemPropertyIsCloudItem]

but can't get it to work as I explained above. Any suggestions?

Kalikalian answered 18/10, 2013 at 13:3 Comment(3)
I've had a similar issue. Apple's Music app filters out all songs that are not on the device when your internet is down, and I don't think it has anything to do with MPMediaItemPropertyIsCloudItem. This property will only grab the songs that you've manually downloaded from the cloud, whereas the Music app is able to detect which songs have been downloaded to the cache regardless of whether it has been downloaded manually or the user has just listened to it once. I'd like to find a solution to this too.Shericesheridan
Or are you simply trying to retrieve songs that have been downloaded from the cloud?Shericesheridan
Sounds like we are looking for the same thing... ;)Kalikalian
K
15

I solved it myself by adding the following line in the viewDidLoad method between MPMediaQuery... and NSArray...

[everything addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithBool:NO] forProperty:MPMediaItemPropertyIsCloudItem]];
Kalikalian answered 21/10, 2013 at 10:9 Comment(0)
I
0

Initialize MPMediaQuery like this in ViewDidLoad method.

MPMediaQuery * everything = [MPMediaQuery songsQuery];
self.songsList = [everything items];
[self.tableView reloadData];

and Cell for Index method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellIdentifier = @"TableCellID";
    TableCellTitle *tablecell = (TableCellTitle *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    MPMediaItem *anItem = (MPMediaItem *)[self.songsList objectAtIndex: indexPath.row];

    if([anItem valueForProperty:MPMediaItemPropertyTitle]) {
       tablecell.cellSongname.text = [anItem valueForProperty:MPMediaItemPropertyTitle];

    }

    return tablecell;

}
Infallibilism answered 18/10, 2013 at 13:35 Comment(1)
This still gives the same output - Everything (local and "cloud" songs)Kalikalian

© 2022 - 2024 — McMap. All rights reserved.