如何存储用户在 iPhone 应用程序中选择的歌曲并稍后检索并播放?

发布于 2024-09-16 09:02:52 字数 1417 浏览 3 评论 0原文

我正在深入研究 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]; }

现在此代码允许您选择一首歌曲并在应用程序运行时随时播放它。我的问题是...

  1. 如何将该歌曲信息存储在 userInfo 字典中,该字典作为代表我的闹钟被触发的本地通知的一部分包含在内?
  2. 我的另一个问题是,一旦我能够从本地通知中检索该歌曲信息,我该如何播放它?

我对这一切都很陌生,我真的很难理解它是如何工作的。预先非常感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文