如何存储用户在 iPhone 应用程序中选择的歌曲并稍后检索并播放?
我正在深入研究 iOS 开发,并一直在慢慢构建自己的闹钟应用程序,以学习如何在该平台上进行开发。我希望我的闹钟能够在我的 iOS 设备上显示歌曲列表,仅选择一首,并在闹钟响时播放它。我已经弄清楚如何使用 MPMediaPicker
来显示歌曲列表,并允许用户选择最终添加到 MPMediaItemCollection
中的歌曲,用于告诉MPMediaPlayer 对象要播放哪些歌曲。这是所有这些的代码...
- (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]; }
存储歌曲...
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection {
[self dismissModalViewControllerAnimated: YES];
selectedSongCollection=mediaItemCollection; }
关闭选择器...
- (void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker {
[self dismissModalViewControllerAnimated: YES]; }
现在此代码允许您选择一首歌曲并在应用程序运行时随时播放它。我的问题是...
- 如何将该歌曲信息存储在
userInfo
字典中,该字典作为代表我的闹钟被触发的本地通知的一部分包含在内? - 我的另一个问题是,一旦我能够从本地通知中检索该歌曲信息,我该如何播放它?
我对这一切都很陌生,我真的很难理解它是如何工作的。预先非常感谢您的帮助!
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...
- 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? - 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
返回的集合中的representativeItem保存到用户信息字典中
当您想要播放歌曲时,将
,使用MPMediaQuery获取要播放的特定歌曲。 http://developer.apple.com/iphone/library/documentation/mediaplayer/reference/MPMediaQuery_ClassReference/Reference/Reference.html#//apple_ref/doc/uid/TP40008220
有关如何查询存储的歌曲的详细信息
http://developer.apple.com/iphone/library/documentation/mediaplayer/reference/MPMediaPropertyPredicate_ClassReference/Reference/Reference.html#//apple_ref/occ/clm/MPMediaPropertyPredicate/predicateWithValue:forProperty:
用于查询数据的 Apple 文档以及示例
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