1

Bellow is code for fetching items in the iPod library of the iOS device, the problem I am having is to do with getting the right MPMediaItemProperty. The way I understand it if I want to get data like artwork, comments title - I gotta take one media item of the MPMediaItemCollection class in my - (MPMediaItem *)mediaItemForRow: (NSInteger)row method.

The problem with this is that I do not get the same info as Apple have - cause I´v checked in their podcast app. They must use some other way of getting the data since I do only get comments from each indivdual podcast episode. and Also I do only get artwork for certain podcasts. While in the podcast app they all have artwork.

So I must be doing something wrong here?

@interface testclassViewController ()
@property (nonatomic, strong)       NSArray                         *audiobooks;
@end

@implementation testclassViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    MPMediaItem *mediaItem = [self mediaItemForRow:0];
    self.testText.text = [mediaItem valueForProperty:MPMediaItemPropertyComments];
    MPMediaItemArtwork *img = [mediaItem valueForProperty:MPMediaItemPropertyArtwork];
    UIImage *artworkUIImage = [img imageWithSize:CGSizeMake (128, 128)];
    self.testImage.image = artworkUIImage;



}


#pragma mark - query settings 

- (MPMediaPropertyPredicate *)mediaTypeToFetchFromiPodLibrary
{
    MPMediaPropertyPredicate *abPredicate =
    [MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInt:MPMediaTypePodcast]
                                     forProperty:MPMediaItemPropertyMediaType];
    return abPredicate;
}


- (MPMediaQuery *)setMediaQueryOptions: (MPMediaQuery*)abQuery
                         withPredicate: (MPMediaPropertyPredicate*) abPredicate
{
    [abQuery addFilterPredicate:abPredicate];
    //[abQuery setGroupingType:MPMediaGroupingAlbum];
    [abQuery setGroupingType:MPMediaGroupingPodcastTitle];
    return abQuery;
}

#pragma mark -

- (MPMediaItem *)mediaItemForRow: (NSInteger)row
{
    NSArray *audiobooks = self.audiobooks;
    MPMediaItem *mediaItem = nil;

    id object = audiobooks[row];
    if ([object isKindOfClass:[MPMediaItemCollection class]]) {
        MPMediaItemCollection *book = (MPMediaItemCollection *)object;

        id item = [book items][0];
        if ([item isKindOfClass:[MPMediaItem class]]) {
            mediaItem = (MPMediaItem *)item;
        }
    }

    return mediaItem;
}


/* Get´s the sub items for Podcasts title */
- (NSArray *)subMediaItemsForPodcastTitle: (NSString *)podcastTitle
{
    NSMutableArray *subMediaItemsToReturn = [NSMutableArray array];
    for (id collections in self.audiobooks) {
        if ([collections isKindOfClass:[MPMediaItemCollection class]]) {
            MPMediaItemCollection *collection = (MPMediaItemCollection *)collections;
            for (id mediaItems in collection.items) {
                MPMediaItem *mediaitem = (MPMediaItem *)mediaItems;

                NSString *mediaItemTitle = [mediaitem valueForProperty:MPMediaItemPropertyPodcastTitle];
                if ([mediaItemTitle isEqual:podcastTitle]) {
                    //NSLog(@"found mediaItem belonging to title: %@",mediaItemTitle);
                    [subMediaItemsToReturn addObject:mediaitem];
                }
            }
        }
    }
    return  subMediaItemsToReturn;
}

// property getter
- (NSArray *)audiobooks
{
    MPMediaPropertyPredicate *abPredicate = [self mediaTypeToFetchFromiPodLibrary];
    MPMediaQuery *abQuery = [[MPMediaQuery alloc] init];
    abQuery = [self setMediaQueryOptions:abQuery withPredicate:abPredicate]; // Abstract
    NSArray *books = [abQuery collections];

    return books;
}

@end
Tom Lilletveit
  • 1,872
  • 3
  • 31
  • 57

2 Answers2

1

If a user downloads the podcast episode using the podcasts app, a lot of the metadata is lost(comments etc). If the user gets the podcast on the device by using iTunes sync, most of the data is there. I've meant to file a bug but haven't. One way to get at some data that might not show up using the MPMediaPlayer framework is to use AVFoundation to get the id3 tag data directly.

#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>


MPMediaItem *item;
AVURLAsset *itemAsset = [item valueForProperty:MPMediaItemPropertyAssetURL];
NSArray *mDatsForFormats = [itemAsset metadataForFormat:@"org.id3"];

for (AVMetadataItem *mDatItem in mDatsForFormats){
    if (mDatItem.stringValue) {
        NSLog(@"\nkey %@\nvalue %@",mDatItem.key,mDatItem.stringValue);
    }
    NSLog(@"%@",mDatItem);

}
dave234
  • 4,793
  • 1
  • 14
  • 29
  • Yes I´v noticed that I get the same data that´s in my iTunes library synced with my mac. But in the podcast / music player app on iOS they get the data from elsewhere – Tom Lilletveit Sep 17 '13 at 12:10
  • @Dave Thanks for the tip! I'm looking for the answer to [a similar question](http://stackoverflow.com/q/29283960/1418688) about iTunes metadata, and I was wondering if this solution could help with that? I'm entirely new to ID3 tags, and "Method 3 of 3" in [this article](http://www.wikihow.com/Convert-Tags-Between-iTunes-and-ID3) seems to indicate that video-related tags aren't shown as ID3 (but are visible? whatever that means?), but is there any chance a method like this could access the "Episode #" and "Season #" properties in an MPMediaItem, etc? – Nerrolken May 31 '15 at 01:48
  • note that in 2017 Swift the format key is not "org.id3" but rather the constant `AVMetadataFormatID3Metadata` – NSTJ Apr 24 '17 at 11:27
  • 1
    In iOS 10, I've found that podcast artwork is only available for episodes which were downloaded using the iOS Podcasts app. Episodes which were synced from iTunes on the Mac did not contain podcast artwork. Hope this helps someone else. – lifjoy Jun 06 '17 at 19:16
  • @lifjoy Thanks, that's what I wanted to know. Might be worth giving that as an answer, because comments are volatile and not readily searchable. – matt Jun 19 '18 at 16:15
0

In iOS 10, I've found that podcast artwork is only available for episodes which were downloaded using the iOS Podcasts app. Episodes which were synced from iTunes on the Mac did not contain podcast artwork. Hope this helps someone else. (Added this as an answer since comments are volatile and not readily searchable).

lifjoy
  • 2,158
  • 21
  • 19