Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to persist MPMediaItemCollection objects? (selected from iPod)

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

like image 743
Adam Waite Avatar asked Nov 23 '11 18:11

Adam Waite


2 Answers

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.

like image 160
jsd Avatar answered Oct 23 '22 18:10

jsd


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.

like image 27
yano Avatar answered Oct 23 '22 20:10

yano