2013-10-18 15 views
8

Desidero visualizzare un UITableView con solo i brani attualmente sul dispositivo.MPMediaQuery per restituire solo gli articoli locali

Se interrogo tutti gli elementi ottengo (ovviamente) tutti gli elementi, incluso quello che ho acquistato ma non scaricato. Ecco il (parte) del codice

@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; 
} 

Ho letto un paio di cose di coinvolgere

[song valueForProperty:MPMediaItemPropertyIsCloudItem] 

, ma non riesco a farlo funzionare come ho spiegato sopra. Qualche suggerimento?

+0

I' ho avuto un problema simile L'app Apple's Music filtra tutti i brani che non si trovano sul dispositivo quando Internet non funziona, e non penso che abbia qualcosa a che fare con 'MPMediaItemPropertyIsCloudItem'. Questa proprietà catturerà solo le canzoni che hai scaricato manualmente dal cloud, mentre l'app Music è in grado di rilevare quali brani sono stati scaricati nella cache, indipendentemente dal fatto che sia stata scaricata manualmente o che l'utente abbia appena ascoltato una volta . Mi piacerebbe trovare una soluzione anche a questo. – sooper

+0

Oppure stai semplicemente cercando di recuperare i brani che sono stati scaricati dal cloud? – sooper

+0

Sembra che stiamo cercando la stessa cosa ...;) – ASCJU

risposta

14

ho risolto io stesso aggiungendo la seguente riga nel metodo viewDidLoad tra il MPMediaQuery ... e NSArray ...

[everything addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithBool:NO] forProperty:MPMediaItemPropertyIsCloudItem]]; 
0

Inizializza MPMediaQuery in questo modo nel metodo ViewDidLoad.

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

e cellulare per il metodo Index

- (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

Questo dà ancora lo stesso risultato - Tutto (canzoni locali e "cloud") – ASCJU

Problemi correlati