Thursday, 27 June 2013

Fetching the images and title of songs and albums ( MPMediaItem ) in the table view. Music Player iOS.


If you want to using default Music player in your app, you can fetch the images of the related song or album using various properties from "MPMediaItemProperty" like MPMediaItemPropertyAlbumTitle, MPMediaItemPropertyArtist, MPMediaItemPropertyTitle, MPMediaItemArtwork.



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //static NSString *CellIdentifier = @"Cell";
    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d",indexPath.section,indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
    }

    MPMediaItem* mediaItem = [_arrPlaylistOfMediaItems objectAtIndex:indexPath.row] ; 
    //_arrPlaylistOfMediaItems is the array of MPMediaItems 
    NSString* mediaTitle = [mediaItem valueForProperty:MPMediaItemPropertyTitle];
    NSString* title = [[NSString alloc]initWithFormat:@"%@",(mediaTitle.length>0 ? mediaTitle : @"Unknown")];
    NSString* artist = [mediaItem valueForProperty:MPMediaItemPropertyArtist];
    NSString* albumTitle = [mediaItem valueForProperty:MPMediaItemPropertyAlbumTitle];
    NSString* subTitle = [[NSString alloc]initWithFormat:@"%@ - %@",(artist.length>0 ? artist : @"Unknown"),(albumTitle.length>0 ? albumTitle : @"Unknown")];

    cell.textLabel.text = title;
    cell.detailTextLabel.text = subTitle;
    cell.imageView.image = _defaultImg;//[UIImage imageNamed: @"iconThumb"];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        MPMediaItemArtwork* artwork = [mediaItem valueForProperty:MPMediaItemPropertyArtwork];
        if ( !artwork || artwork.bounds.size.width == 0 ) {
            // try album artwork
            NSNumber* albumID = [mediaItem valueForProperty:MPMediaItemPropertyAlbumPersistentID];
            MPMediaQuery*   mediaQuery = [MPMediaQuery albumsQuery];
            MPMediaPropertyPredicate* predicate = [MPMediaPropertyPredicate predicateWithValue:albumID forProperty:MPMediaItemPropertyAlbumPersistentID];
            [mediaQuery addFilterPredicate:predicate];
            NSArray* arrMediaItems = [mediaQuery items];
            if ( [arrMediaItems count] > 0 ) {
                artwork = [[arrMediaItems objectAtIndex:0] valueForProperty:MPMediaItemPropertyArtwork];
                if ( artwork ) {
                    int nBreak = 0;
                    nBreak++;
                }
            }
        }
       dispatch_sync(dispatch_get_main_queue(), ^{
            if ( artwork && artwork.bounds.size.width > 0 ) {
                      cell.imageView.image = [self scale: [artwork imageWithSize:CGSizeMake (50, 50)] toSize:CGSizeMake(55, 55)] ;
                cell.imageView.image =[self scale:[artwork imageWithSize:CGSizeMake(50,50)]toSize:CGSizeMake(45,45)];
            }
            else {
                 NSLog(@"Bad artwork: %@ - %@ - %f-%f", strTitle, strsubTitle, artwork.bounds.size.width, artwork.bounds.size.height);
                
                cell.imageView.image = _defaultImg;
            }
       });
    });
   return cell;
}

No comments:

Post a Comment