2011-04-18 10 views
8

ahorré el ID persistente de las canciones seleccionadas con el selector de medios de comunicación, ahora estoy tratando de caja de la cola para jugar esa lista, pero algo está mal en este código:Juega iPod lista de reproducción recuperado de una lista persistentid salvado

myPlayer = [MPMusicPlayerController applicationMusicPlayer]; 

    NSMutableArray *canzonilist = [[[NSMutableArray alloc] initWithCapacity:1] retain]; 
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"listaCanzoni"]; 
    NSArray *decodedData = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 
    [canzonilist addObjectsFromArray:decodedData]; 

    MPMediaQuery *songQuery = [MPMediaQuery songsQuery]; 
    for (int i = 0; i > [canzonilist count]; i++) { 
     [songQuery addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:[canzonilist objectAtIndex:i] forProperty:MPMediaItemPropertyPersistentID]]; 
    } 
    NSArray *songs = [songQuery items]; 
    MPMediaItemCollection *currentQueue = [[MPMediaItemCollection alloc] initWithItems:songs]; 

    if (currentQueue != nil) {  
     [myPlayer setQueueWithItemCollection:currentQueue]; 
    } else { 
     [myPlayer setQueueWithQuery: [MPMediaQuery songsQuery]]; 
    } 

    [myPlayer play]; 
    [canzonilist release]; 
    [currentQueue release]; 

Gracias

Respuesta

21

fin he encontrado la solución:

GUARDAR:

LOAD

- (void)setupMusic { 

    myPlayer = [MPMusicPlayerController applicationMusicPlayer]; 

    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"songsList"] != nil) { 

     NSMutableArray *theList = [[NSMutableArray alloc] initWithCapacity:0]; 

     NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"songsList"]; 

     NSArray *decodedData = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 

     [theList addObjectsFromArray:decodedData]; 

     NSMutableArray *allTheSongs = [[NSMutableArray alloc] initWithCapacity:0]; 

     for (int i = 0; i < [theList count]; i++) { 

      MPMediaQuery *songQuery = [MPMediaQuery songsQuery]; 

      [songQuery addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:[theList objectAtIndex:i] forProperty:MPMediaItemPropertyPersistentID]]; 

      NSArray *songs = [songQuery items]; 

      [allTheSongs addObjectsFromArray: songs]; 

     } 

     MPMediaItemCollection *currentQueue = [[MPMediaItemCollection alloc] initWithItems:allTheSongs]; 

     [myPlayer setQueueWithItemCollection:currentQueue]; 

     [theList release]; 

     [currentQueue release]; 

     [allTheSongs release]; 

    } else { 

     [myPlayer setQueueWithQuery: [MPMediaQuery songsQuery]]; 

     [myPlayer setShuffleMode: MPMusicShuffleModeSongs]; //optional 

    } 

} 
+0

super thanks :) – Pericles

Cuestiones relacionadas