开发者

How can I store the song the user chose in my iPhone app and later retrieve it and play it?

I'm diving into iOS development and have been slowly building my own alarm clock app to learn how to develop on the platform. I want my alarm clock to allow me to display a list of songs on my iOS device, choose only one, and have it play when the alarm fires. I've figured out how to use the MPMediaPicker to display the list of songs and allow the user to select songs that are ultimately added to a MPMediaItemCollection that is used to tell the MPMediaPlayer object which songs to play. Here's the code for all that...

- (IBAction) selectSong: (id) sender {  
MPMediaPickerController *picker =
[[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];

picker.delegate     开发者_高级运维                                    = self;
picker.allowsPickingMultipleItems       = NO;
picker.prompt                                           = NSLocalizedString (@"Select any song from the list", @"Prompt to user to choose some songs to play");

[self presentModalViewController: picker animated: YES];
[picker release]; }

Store the song...

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

[self dismissModalViewControllerAnimated: YES];
selectedSongCollection=mediaItemCollection; }

Dismiss the picker...

- (void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker {

[self dismissModalViewControllerAnimated: YES]; }

Now this code allows you to choose a song and play it at any point while the app is running. My questions are...

  1. How can I store that song information in the userInfo dictionary that's included as part of the local notification that represents my alarm being triggered?
  2. my other question is, once I'm able to retrieve that song info from the local notification, how do I play it?

I'm so new to all this that I'm really having a hard time understanding how this would work. Thanks so much in advance for your help!


save the representativeItem from the collection returned to user info dictionary

when you want to play the song back, use MPMediaQuery to get the specific song to play.

http://developer.apple.com/iphone/library/documentation/mediaplayer/reference/MPMediaQuery_ClassReference/Reference/Reference.html#//apple_ref/doc/uid/TP40008220

details on how to query for the stored song

http://developer.apple.com/iphone/library/documentation/mediaplayer/reference/MPMediaPropertyPredicate_ClassReference/Reference/Reference.html#//apple_ref/occ/clm/MPMediaPropertyPredicate/predicateWithValue:forProperty:

Apple Documentation for Querying data, plus examples

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜