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.
#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);
}