播放从保存的 persistentid 列表中检索到的 iPod 播放列表

Posted

技术标签:

【中文标题】播放从保存的 persistentid 列表中检索到的 iPod 播放列表【英文标题】:Play iPod playlist retrieved from a saved persistentid list 【发布时间】:2011-04-18 12:15:28 【问题描述】:

我保存了使用媒体选择器选择的歌曲的持久 ID,现在我正在尝试创建队列以播放该列表,但这段代码有问题:

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

谢谢

【问题讨论】:

【参考方案1】:

终于找到了解决办法:

保存:

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




加载

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

     


【讨论】:

以上是关于播放从保存的 persistentid 列表中检索到的 iPod 播放列表的主要内容,如果未能解决你的问题,请参考以下文章

MPMediaItem 的唯一 ID,而不是 persistentID

如何从 NSUserDefaults 中检索数据?

iPhone 4 iOS 5 如何使用 Core Data 保存 MPMediaItem?

使用 YouTube API v3 检索播放列表中的项目数

如何将 MPMediaItemCollection 保存到数据库?

通过 Youtube API (python) 检索 Youtube 播放列表信息