How to get the MPMediaItem (audiobook) individual

2019-05-21 05:51发布

I know I can get all the audiobooks from the iPod library with:

   MPMediaPropertyPredicate *abPredicate =
    [MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInt:MPMediaTypeAudioBook] 
                                     forProperty:MPMediaItemPropertyMediaType];

    MPMediaQuery *abQuery = [[MPMediaQuery alloc] init];
    [abQuery addFilterPredicate:abPredicate];
    [abQuery setGroupingType:MPMediaGroupingAlbum];
    NSArray *books = [abQuery collections];

And I can get the parts/files for each book by using this:

 [book items];

What I cant figure out is how to get the separate chapters that make up each part.

I know you can see this in the iPod application by tapping the "track" button in the upper right corner while playing a book. This flips the player around and shows the list of chapters.

Is apple using a private API to get this?

1条回答
Melony?
2楼-- · 2019-05-21 06:14

To get the individual chapters you need to create an AVAsset from the MPMediaItem's AssetURL property.

NSURL *assetURL = [mediaItem valueForProperty:MPMediaItemPropertyAssetURL];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
NSArray *locales = [asset availableChapterLocales];
NSArray *chapters = [asset chapterMetadataGroupsWithTitleLocale:locale containingItemsWithCommonKeys:[NSArray arrayWithObject:AVMetadataCommonKeyArtwork]];

Get the url and create the asset, check the available locales for the chapter, and get the chapters from the asset. The result is an array of AVTimedMetadataGroups that each contain a CMTimeRange and an array of AVMetadataItems. Each AVMetadataItem holds a piece of metadata (e.g. chapter title, chapter artwork).

According to the documentation the only supported key is the AVMetadataCommonKeyArtwork.

查看更多
登录 后发表回答