同步后立即从 iPod 库加载歌曲
我正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现实际上有一种方法可以查看更新何时完成。更新结束后,设备会发布通知。
唯一的问题是我无法找到一种方法来确定设备是否正在更新 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.
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.
@Peter 是对的 - 实际上我找到了解决他的问题的方法。
起初我发现
MPMediaPickerController
在同步时分配和启动时返回nil
- 起初我认为它可以检查是否有对库的访问,但有时它不会'不工作。目前我发现的唯一方法是检查
MPMediaLibrary
的lastModificationDate
- 只要它发生变化,您就不会使用MPMediaQuery
获得结果 - 延迟当该属性停止变化(以任何您喜欢的方式)时,您的更改应该没问题。已经发送了有关该问题的错误报告 - 文档说您应该在通知触发时从库中重新加载缓存的对象,但如果
MPMediaQuery
返回nil
,您显然无法做到这一点你试图寻找的每一个物体。@Peter is right - and actually I found a walkaround for his problem.
At first I found that
MPMediaPickerController
returnsnil
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
ofMPMediaLibrary
- as long as it's changing you won't get results usingMPMediaQuery
- 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
returnsnil
for every object you try to find.