2011-02-01 32 views
5

Cố đoạn mã sau:Nhận năm album cho mục trong thư viện iPod?

// Per albums 
MPMediaQuery *albumsQuery = [MPMediaQuery albumsQuery]; 
NSArray *collections = [albumsQuery collections]; 

for (MPMediaItemCollection *collection in collections) 
{ 
    NSDate *collectionReleaseDate = [collection valueForProperty: MPMediaItemPropertyReleaseDate]; 
    NSLog(@"collection release date: %@", collectionReleaseDate); 

    MPMediaItem *representativeItem = [collection representativeItem]; 
    NSDate *representativeItemReleaseDate = [representativeItem valueForProperty: MPMediaItemPropertyReleaseDate]; 
    NSLog(@"representativeItem release date: %@", representativeItemReleaseDate); 
} 

// Just per item 
MPMediaQuery *query = [[MPMediaQuery alloc] init]; 
NSArray *items = [query items]; 

for (MPMediaItem *item in items) 
{ 
    NSDate *date = [item valueForProperty: MPMediaItemPropertyReleaseDate]; 
    NSLog(@"release date: %@", date); 
} 

Trong mọi trường hợp tôi nhận được của nil cho NSDates ... Nhưng trong thư viện iPod Tôi có thể thấy ngày tháng, do đó thông tin phải có sẵn. Cách chính xác để có được nó là gì?

+0

ngày Việc phát hành là chỉ có thể sử dụng iOS 4 hoặc cao hơn. Phiên bản mục tiêu của bạn là gì? –

+0

Sử dụng iOS SDK 4.2, mục tiêu triển khai = iOS 4.0, thử nghiệm trên iPad với 4.2.1 và iPod thế hệ thứ 2 với 4.2.1 ... – ivanzoid

Trả lời

19

Vâng, tôi nghĩ tôi đã tìm ra. Tôi đã nghĩ rằng cột 'Năm' trong iTunes tương ứng với MPMediaItemPropertyReleaseDate trong API - nhưng nó sai. Các mục của tôi thực sự không có thông tin ngày phát hành.

Tôi cũng thấy làm thế nào để có được 'Năm' thông tin (mà tôi cần thiết), nhưng tiếc là theo cách không có giấy tờ:

MPMediaItem *item = ...; 
NSNumber *yearNumber = [item valueForProperty:@"year"]; 
if (yearNumber && [yearNumber isKindOfClass:[NSNumber class]]) 
{ 
    int year = [yearNumber intValue]; 
    if (year != 0) 
    { 
     // do something with year 
    } 
} 
+0

Vẫn hợp lệ cho iOS 5.1:/Nhưng cảm ơn bạn đã khắc phục! – miho

+0

không hoạt động trên ios6 – Andy

+0

@Andy Works trên iOS 6.0 dành cho tôi. – soheilpro

Các vấn đề liên quan