iPhone sdk, save MPMediaItemCollection?

后端 未结 2 1563
囚心锁ツ
囚心锁ツ 2021-02-06 10:07

I have my application displaying a MPMediaPickerController. I would like to save the MediaItem and start it playing again on startup. I think this is d

相关标签:
2条回答
  • 2021-02-06 10:42

    Man, you dont need to save mediaCollection. media collection it is just array of MPMediaItem objects. So you'd better save persistentIds of this items. it's quite easy

    //it's how to know persistentId of the song after you got mediaItemCollection from your mediaPickerViewController
    //then you can sav it in userDefaults.
    - (NSNumber *)getPersistentId :(MPMediaItemCollection *)collection atIndex:(int)index {
     MPMediaItem *mediaItem = [collection.items objectAtIndex:index];
     NSNumber *anId = [mediaItem valueForProperty:MPMediaItemPropertyPersistentID];
     return anId;
    }
    
    //when your application will be launched next time you can get required song:
    - (void)obtainSongWitId:(NSNumber *)persistentId {
     MPMediaQuery *query = [MPMediaQuery songsQuery];
     MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:persistentId forProperty:MPMediaItemPropertyPersistentID];
     [query addFilterPredicate:predicate];
     NSArray *mediaItems = [query items];
     //this array will consist of song with given persistentId. add it to collection and play it
     MPMediaItemCollection *col = [[MPMediaItemCollection alloc] initWithItems:mediaItems];
     ///....
     [col release];
    }
    
    0 讨论(0)
  • 2021-02-06 10:44

    This should work:

    MPMediaQuery *query = [MPMediaQuery songsQuery];
    MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:myPersistentID forProperty:MPMediaItemPropertyPersistentID];
    
    [query addFilterPredicate:predicate];
    NSArray *songs = [query items];
    
    0 讨论(0)
提交回复
热议问题