I'm dev开发者_运维问答eloping 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
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.
@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.
精彩评论