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
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.
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).