Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPMediaQuery to return local items only

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?

like image 897
ASCJU Avatar asked Oct 18 '13 13:10

ASCJU


1 Answers

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]];
like image 187
ASCJU Avatar answered Nov 13 '22 06:11

ASCJU