持久化 MPMediaItemCollection 对象的方法? (选自 iPod)

发布于 2024-12-17 14:25:55 字数 1915 浏览 0 评论 0原文

我正在制作一个应用程序,用户可以在其中选择设置选项卡中的歌曲,并根据需要在不同的视图中播放该歌曲。我想要它,以便在用户关闭应用程序并再次重新打开它时可以存储该项目。

我已经设法允许用户选择并存储一首歌曲:

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

但我希望用户每次使用该应用程序时都必须执行此操作。

我尝试使用 NSUserDefaults:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:settingsData.selectedSong forKey:@"alarmSoundKey"];
[defaults synchronize];

但出现错误:

* -[NSUserDefaults setObject:forKey:]: 尝试插入类“MPMediaItemCollection”的非属性值“”。请注意,属性列表中的字典和数组也必须仅包含属性值。

请问我有什么选择?不太确定如何解决这个问题...

解决方案 -

我还无法回答我自己的问题,所以我将其放在这里:

我已经找到了自己的解决方案:

首先将 MPMediaItemCollection 转换/编码为 NSData 对象并使用 NSUserDefaults slam 存储它:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:mediaItemCollection];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:data forKey:@"someKey"];
[defaults synchronize];

从那里,您可以解码并在应用程序中的其他任何地方使用...

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:@"someKey"];
MPMediaItemCollection *mediaItemCollection = [NSKeyedUnarchiver unarchiveObjectWithData:data]

希望这对某人有帮助。传播一下,这还不够。实际上已经在这个问题上工作了大约 4 个小时......

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

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

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静水深流 2024-12-24 14:25:57

您只能将属性列表值存储在 NSUserDefaults 中。由于 MPMediaItemCollection 符合 NSCoding,因此您可以使用 NSKeyedArchiver 来存储它。

http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSKeyedArchiver_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003672

然后,您可以使用 NSKeyedUnarchiver 将其从文件中读回。

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.

猥︴琐丶欲为 2024-12-24 14:25:57

您还可以使用 MPMediaItemPropertyPersistentID 属性。您可以形成一个查询,以便在您的应用程序下次启动时从 iPod 库中检索该项目,并优雅地处理诸如用户决定从其库中删除歌曲时之类的事情。

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.

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