MPMediaQuery to return local items only

前端 未结 2 513
没有蜡笔的小新
没有蜡笔的小新 2021-02-09 02:18

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 purchas

相关标签:
2条回答
  • 2021-02-09 02:48

    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;
    
    }
    
    0 讨论(0)
  • 2021-02-09 02:49

    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]];
    
    0 讨论(0)
提交回复
热议问题