2013-10-18 22 views
8

Tôi muốn hiển thị UITableView chỉ với các bài hát hiện có trên thiết bị.MPMediaQuery chỉ trả về các mục địa phương

Nếu tôi truy vấn tất cả các mục, tôi (rõ ràng) nhận tất cả các mục, kể cả khi tôi đã mua nhưng chưa tải xuống. Đây là (một phần) của mã

@property (strong, nonatomic) NSMutableArray *songsList; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.tableView.delegate = self; 
    self.tableView.dataSource = self; 
    MPMediaQuery *everything = [[MPMediaQuery alloc] init]; 
    NSArray *itemsFromGenericQuery = [everything items]; 
    self.songsList = [NSMutableArray arrayWithArray:itemsFromGenericQuery]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return self.songsList.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"TableCellID"; 
    TableCellTitle *tablecell = (TableCellTitle *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
MPMediaItem *song = [self.songsList objectAtIndex:indexPath.row]; 

    NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle]; 
    tablecell.cellSongname.text = songTitle; 

    return tablecell; 
} 

Tôi đã đọc một vài điều về sự tham gia của

[song valueForProperty:MPMediaItemPropertyIsCloudItem] 

nhưng không thể làm cho nó làm việc như tôi đã giải thích ở trên. Bất kỳ đề xuất nào?

+0

tôi đã có một vấn đề tương tự. Ứng dụng âm nhạc của Apple lọc ra tất cả các bài hát không có trên thiết bị khi internet của bạn ngừng hoạt động và tôi không nghĩ rằng nó có liên quan đến 'MPMediaItemPropertyIsCloudItem'. Thuộc tính này sẽ chỉ lấy các bài hát bạn đã tải xuống từ đám mây, trong khi ứng dụng Nhạc có thể phát hiện bài hát nào đã được tải xuống bộ nhớ cache bất kể bài hát đã được tải xuống theo cách thủ công hay người dùng vừa nghe nó một lần . Tôi cũng muốn tìm một giải pháp cho điều này. – sooper

+0

Hoặc bạn chỉ đang cố truy xuất các bài hát đã được tải xuống từ đám mây? – sooper

+0

Nghe có vẻ như chúng tôi đang tìm kiếm cùng một điều ...;) – ASCJU

Trả lời

14

Tôi giải quyết nó bản thân mình bằng cách thêm dòng sau trong phương pháp viewDidLoad giữa MPMediaQuery ... và NSArray ...

[everything addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithBool:NO] forProperty:MPMediaItemPropertyIsCloudItem]]; 
0

Khởi tạo MPMediaQuery như thế này trong phương thức ViewDidLoad.

MPMediaQuery * everything = [MPMediaQuery songsQuery]; 
self.songsList = [everything items]; 
[self.tableView reloadData]; 

và di động cho phương pháp Index

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *cellIdentifier = @"TableCellID"; 
    TableCellTitle *tablecell = (TableCellTitle *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    MPMediaItem *anItem = (MPMediaItem *)[self.songsList objectAtIndex: indexPath.row]; 

    if([anItem valueForProperty:MPMediaItemPropertyTitle]) { 
     tablecell.cellSongname.text = [anItem valueForProperty:MPMediaItemPropertyTitle]; 

    } 

    return tablecell; 

} 
+0

Điều này vẫn mang lại cho cùng một đầu ra - Tất cả mọi thứ (địa phương và "đám mây" bài hát) – ASCJU

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