开发者

Need to store a song from the iPod in NSUserDefaults. Nearly figured it out, need help getting there though?

So this is what i have so开发者_如何转开发 far:

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

    [self dismissModalViewControllerAnimated:YES];
    // Assign the selected item(s) to the music player and start playback.
self.selectedSong = mediaItemCollection;

    MPMediaItem *chosenItem = mediaItemCollection.representativeItem;
    NSLog(@"chosenItem: %@", chosenItem);
    persistentID = [chosenItem valueForProperty:MPMediaItemPropertyPersistentID]; //persistentID is an NSString.
    NSLog(@"persistentID: %@", persistentID);
    [prefs setValue:persistentID forKey:@"persistentIDKey"];
    [prefs synchronize];

}

But now i need to figure out what to put in the part of the script where i want it retrieved. This would be the standard way of doing that..

[self.musicPlayer stop];
[self.musicPlayer setNowPlayingItem:*ITEM HERE*];
[self.musicPlayer play];

But i'm unsure what to put in there. Persistent ID doesn't work. I can also get any other properties from the song using my above method, not just persistentID. So if i require any others, then i can get those too. Help with this?


You should be able to do something like this:

MPMediaPropertyPredicate * predicate = [MPMediaPropertyPredicate predicateWithValue:storedPID forProperty: MPMediaItemPropertyPersistentID];
MPMediaQuery * query = [[MPMediaQuery alloc] initWithFilterPredicates: [NSSet setWithObject: predicate]];
[self.musicPlayer setQueueWithQuery:query];
[self.musicPlayer play];
[query release];


Just be aware, as per Apple's docs:

The value of the MPMediaItemPropertyPersistentID identifier persists across application launches and across syncs that do not change the sync status of the media item.The value is not guaranteed to persist across a sync/unsync/sync cycle.

I have never come across an instance where this is has been an issue, however this has been the case since MPMediaItemPropertyPersistentID was introduced in iOS 3.0

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜