同步后立即从 iPod 库加载歌曲

发布于 2024-09-16 07:34:26 字数 739 浏览 4 评论 0原文

我正在开发一个 iPhone 应用程序,它使用 iPod 库来播放一些歌曲。我用下面的代码加载歌曲。问题是,在设备与 iTunes 同步后立即运行此代码时,会出现问题。显然 iPod 库需要更新,并且需要一些时间。如果我在同步后立即访问 iPod 应用程序,我会看到一条消息“正在更新库..”。如果我在发生这种情况时从应用程序中调用“[查询项目]”,我会得到一个空数组,表明库中没有歌曲。更新结束后一切都很完美。有什么办法可以解决这个问题吗?也许是一种检测更新何时结束的方法。我尝试监听所有 NSNotifications,但更新完成后没有人调用。

    MPMediaQuery *query = [MPMediaQuery songsQuery];

 // convert all items to abstracted media item
 NSArray *items = [query items];

 NSMutableArray *convertedItems = [[NSMutableArray alloc] initWithCapacity:[items count]];
 for (MPMediaItem *item in items) {
  REMediaItem *mediaItem = [[REMediaItem alloc] initWithMediaItem:item];
  [convertedItems addObject:mediaItem];
  [mediaItem release];
 }

我希望有人能提供帮助。

彼得

I'm developing an iPhone application that uses the iPod library to play some songs. I load the songs with the code below. The problem is, when running this code right after the device has been synced with iTunes, there is a problem. Apparently the iPod Library needs to be updated, and it takes some time. If I go to the iPod Application right after a sync I seen a message saying "Updating Library..". If i call "[query items]" from my application while that is happening, I get an empty array indicating there is no songs in the library. Everything works perfect when the update is over. Is there any way to solve this problem? Maybe a way to detect when the update is over. I have tried to listen to alle NSNotifications, but none were called when the update finished.

    MPMediaQuery *query = [MPMediaQuery songsQuery];

 // convert all items to abstracted media item
 NSArray *items = [query items];

 NSMutableArray *convertedItems = [[NSMutableArray alloc] initWithCapacity:[items count]];
 for (MPMediaItem *item in items) {
  REMediaItem *mediaItem = [[REMediaItem alloc] initWithMediaItem:item];
  [convertedItems addObject:mediaItem];
  [mediaItem release];
 }

I hope someone can help.

Peter

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

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

发布评论

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

评论(2

枯寂 2024-09-23 07:34:26

我发现实际上有一种方法可以查看更新何时完成。更新结束后,设备会发布通知。

[[MPMediaLibrary defaultMediaLibrary] beginGeneratingLibraryChangeNotifications]
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self 
                       selector:@selector(iPodLibraryDidChange)
                           name: MPMediaLibraryDidChangeNotification 
                         object:nil];

唯一的问题是我无法找到一种方法来确定设备是否正在更新 iPod 库,我应该等待它完成,或者设备库中根本没有任何歌曲。 [query items] 在这两种情况下都将返回一个空数组。

I discovered that there actually is a way to see when the update is complete. The device will post a notification when the update is over.

[[MPMediaLibrary defaultMediaLibrary] beginGeneratingLibraryChangeNotifications]
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self 
                       selector:@selector(iPodLibraryDidChange)
                           name: MPMediaLibraryDidChangeNotification 
                         object:nil];

The only problem is that I can't find a way to determinate if the device is updating the iPod Library and I should wait for it to finish or the device simply doesnt have any songs in the library. [query items] will return an empty array in both cases.

白馒头 2024-09-23 07:34:26

@Peter 是对的 - 实际上我找到了解决他的问题的方法。
起初我发现 MPMediaPickerController 在同步时分配和启动时返回 nil - 起初我认为它可以检查是否有对库的访问,但有时它不会'不工作。
目前我发现的唯一方法是检查 MPMediaLibrarylastModificationDate - 只要它发生变化,您就不会使用 MPMediaQuery 获得结果 - 延迟当该属性停止变化(以任何您喜欢的方式)时,您的更改应该没问题。
已经发送了有关该问题的错误报告 - 文档说您应该在通知触发时从库中重新加载缓存的对象,但如果 MPMediaQuery 返回 nil ,您显然无法做到这一点你试图寻找的每一个物体。

@Peter is right - and actually I found a walkaround for his problem.
At first I found that MPMediaPickerController returns nil when allocated and initiated while syncing - at first I thought it will work to check if there's an access to the library but sometimes it doesn't work.
The only way for now I found is to check lastModificationDate of MPMediaLibrary - as long as it's changing you won't get results using MPMediaQuery - delay your changes to a moment when that property stops changing (by any way you like) and you should be fine.
Already sent a bug report on that - the documentation says you should reload your cached objects from library when the notification fires but you clearly can't do it if MPMediaQuery returns nil for every object you try to find.

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