Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

     }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...