Way to persist MPMediaItemCollection objects? (selected from iPod)

◇◆丶佛笑我妖孽 提交于 2019-12-04 12:06:26

问题


I am making an app in which the user can select a song in a settings tab and have this played in a different view on demand. I want it so that this item can be stored if the user is to shut the app and reopen it another time.

I have managed to allow the user to select and store a song in with:

-(IBAction)showMediaPicker:(id)sender{

    MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeAny];

    mediaPicker.delegate = self;

    mediaPicker.allowsPickingMultipleItems = NO;

    mediaPicker.prompt = @"Select Alarm Sound";

    [self presentModalViewController:mediaPicker animated:YES];

}


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

    [self dismissModalViewControllerAnimated: YES];

    settingsData.selectedSong = mediaItemCollection;//Object of type MPMediaItemCollection

but I want the user to have to do this every time they use the app.

I have tried using NSUserDefaults:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:settingsData.selectedSong forKey:@"alarmSoundKey"];
[defaults synchronize];

but get the error:

* -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '' of class 'MPMediaItemCollection'. Note that dictionaries and arrays in property lists must also contain only property values.

What are my options please? Not really sure how to tackle this one...

SOLUTION -

I can't answer my own questions yet so I'll put it up here:

I HAVE FOUND MY OWN SOLUTION TO THIS:

First convert/encode the MPMediaItemCollection to an NSData Object and slam store it using NSUserDefaults using:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:mediaItemCollection];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:data forKey:@"someKey"];
[defaults synchronize];

From there, you can decode and use anywhere else in your app....

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:@"someKey"];
MPMediaItemCollection *mediaItemCollection = [NSKeyedUnarchiver unarchiveObjectWithData:data]

Hope that is some help to someone. Spread the word, this hasn't been covered enough. Have literally been working on this problem for about 4 hours...


回答1:


You can only store property list values in NSUserDefaults. Since MPMediaItemCollection conforms to NSCoding you could use an NSKeyedArchiver to store it instead.

http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSKeyedArchiver_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003672

You then use NSKeyedUnarchiver to read it back out of the file later.




回答2:


You can also use the MPMediaItemPropertyPersistentID property. You can form a query to retrieve the item from the iPod library when your application next launches, and gracefully handle things like when the user decides to remove the song from their library.



来源:https://stackoverflow.com/questions/8247315/way-to-persist-mpmediaitemcollection-objects-selected-from-ipod

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