iPhone sdk, save MPMediaItemCollection?

你。 提交于 2019-12-20 14:20:25

问题


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 doable using the MPMediaQuery. It seems, I should be using the MPMediaItemPropertyPersistentID but I am not sure how to query for it. Any thoughts?


回答1:


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



回答2:


This should work:

MPMediaQuery *query = [MPMediaQuery songsQuery];
MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:myPersistentID forProperty:MPMediaItemPropertyPersistentID];

[query addFilterPredicate:predicate];
NSArray *songs = [query items];


来源:https://stackoverflow.com/questions/1745739/iphone-sdk-save-mpmediaitemcollection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!