Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play iPod playlist retrieved from a saved persistentid list

I saved the persistent ID of the songs selected with media picker, now I'm trying to crate the queue to play that list but something is wrong in this code:

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];

Thanks

like image 420
Enlil Avatar asked Apr 18 '11 12:04

Enlil


Video Answer


1 Answers

Finally I found the solution:

SAVE:

    - (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection {

    [self savePlaylist:mediaItemCollection];

    [self dismissModalViewControllerAnimated: YES];

}

- (void)savePlaylist:(MPMediaItemCollection *) mediaItemCollection {

    NSArray* items = [mediaItemCollection items];

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

    for (MPMediaItem *song in items) {

        NSNumber *persistentId = [song valueForProperty:MPMediaItemPropertyPersistentID];

        [listToSave addObject:persistentId];

    }

    NSData *data = [NSKeyedArchiver archivedDataWithRootObject: listToSave];

    [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"songsList"];

    [[NSUserDefaults standardUserDefaults] synchronize];

    [listToSave release];



}

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

     }

}
like image 126
Enlil Avatar answered Sep 20 '22 20:09

Enlil