0

Je peux afficher la bibliothèque musicale sur UITableView.Lecture de musique lorsque je sélectionne UITableViewCell

songsArray = [[NSMutableArray alloc]init]; 
MPMediaQuery *playlistQuery = [[MPMediaQuery alloc]init]; 
[playlistQuery setGroupingType:MPMediaGroupingTitle]; 
songArray = [playlistQuery items]; 
for (MPMediaItem *song in songArray) { 
    NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];   
    [songsArray addObject:songTitle]; 
} 
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    (NSIndexPath *)indexPath { 
    cell.textLabel.text = [songsArray objectAtIndex:row]; 
[cell.textLabel setTextColor:[UIColor whiteColor]]; 

Mais je ne peux pas lire de musique lorsque j'ai sélectionné UITableView Cell.

NSUInteger row = indexPath.row; 

NSString *selectedSong = [songsArray objectAtIndex:row]; 
MPMusicPlayerController *appPlayer = [MPMusicPlayerController iPodMusicPlayer]; 

[appPlayer setQueueWithQuery:selectedSong]; 

[appPlayer play]; 

}

Qui peut me aider?

+0

Est-ce que le travail de MPMusiePlayerController sans tableView, si vous manuellement sel ect une chanson? –

+0

Oui, je sélectionne une chanson sans MediaPicker. – Sookcha

+0

Je vais choisir une chanson comme le lecteur de musique par défaut de l'iPhone (iPod) – Sookcha

Répondre

0

Ah je vois le problème:

Déplacer ce code:

NSUInteger row = indexPath.row; 
NSString *selectedSong = [songsArray objectAtIndex:row]; 
MPMusicPlayerController *appPlayer = [MPMusicPlayerController iPodMusicPlayer]; 
[appPlayer setQueueWithQuery:selectedSong]; 
[appPlayer play]; 

la méthode du protocole UITableViewDelegate 'didSelectRowAtIndexPath' comme ceci:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSUInteger row = indexPath.row; 
    NSString *selectedSong = [songsArray objectAtIndex:row]; 
    MPMusicPlayerController *appPlayer = [MPMusicPlayerController iPodMusicPlayer]; 
    [appPlayer setQueueWithQuery:selectedSong]; 
    [appPlayer play]; 
} 

Modifier

Documentation:

+0

Merci, mais il a un avertissement. – Sookcha

+0

types Objective-C 'struct NSString *' incompatibles, attendu 'struct MPMediaQuery *' lors du passage de l'argument 1 de 'setQueueWithQuery:' à partir du type Objective-C distinct. Qu'est-ce qui ne va pas? – Sookcha

+0

setQueueWithQuery attend un objet MPMediaQuery. S'il vous plaît se référer à la documentation d'Apple. –

Questions connexes