0

I am trying to save a playlist of songs created with the media picker. I tried to use the suggestion provided in Persist a MPMediaItemCollection Object Using NSUserDefaults. This solution uses NSKeyedArchiver as follows:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:mediaItemCollection];

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

Then when the MPMediaItemCollection needs to be retrieved, the following is performed:

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

While this seemed to work at first, I found that it doesn't work consistently over time. It seems that after my app is loaded after a new software release, some of the media items in the playlist are corrupted. For example, when I try to load the song title from the retrieved MPMediaItemCollection for display in a tableView as follows:

MPMediaItem *anItem = (MPMediaItem *)[mediaItemCollection.items objectAtIndex: row];

if (anItem) {
    cell.textLabel.text = [anItem valueForProperty:MPMediaItemPropertyTitle];

    if (cell.textLabel.text == nil) {
        NSString * persistentID = [anItem valueForProperty:MPMediaItemPropertyPersistentID];
        ...
}

the song title is nil for some of the entries. If I try to grab the persistentID for the song at this index in the queue, there is a persistentID, but it doesn't point to a valid song (so its probably just garbage).

SO THE QUESTION IS: Is it possible to save a MPMediaItemCollection that I can be sure is valid across all launches and software upgrades (both my upgrades and iOS upgrades). If so, how?

4

1 回答 1

0

答案不是这样!MPMediaItemCollection 对象不能转换为 NSArray、NSDictionary 或我们可以在 NSUserDefaults 中存档的任何其他对象。

我的建议是枚举集合,保存一个 s 数组[anItem valueForProperty:MPMediaItemPropertyPersistentID]。将其保存在NSUserDefaults

然后在应用程序的下一次运行中,您读取并枚举 NSArray 并使用具有 persistentID 的项目创建 MPMediaItemCollection。

我希望它有帮助

于 2014-02-08T00:17:03.620 回答