How to get the MPMediaItem (audiobook) individual chapters

…衆ロ難τιáo~ 提交于 2019-12-22 18:27:06

问题


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:


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.



来源:https://stackoverflow.com/questions/9345934/how-to-get-the-mpmediaitem-audiobook-individual-chapters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!