How do I check an MPMediaItem for MPMediaType of just audio?

[亡魂溺海] 提交于 2019-12-21 05:05:28

问题


I expect I need to do a bitwise comparison but I am unclear on how that is done in Objective-C syntax. The enum definition of MPMediaType is below.

What I need to do is ensure the MPMediaItem is not video at all because AVAssetReader is choking on video files despite filtering to MPMediaTypeAnyAudio with my media query.

How can I ensure the MPMediaItem is one of the only audio types?

enum {
    // audio
    MPMediaTypeMusic        = 1 << 0,
    MPMediaTypePodcast      = 1 << 1,
    MPMediaTypeAudioBook    = 1 << 2,
    MPMediaTypeAudioITunesU = 1 << 3, // available in iOS 5.0
    MPMediaTypeAnyAudio     = 0x00ff,

    // video (available in iOS 5.0)
    MPMediaTypeMovie        = 1 << 8,
    MPMediaTypeTVShow       = 1 << 9,
    MPMediaTypeVideoPodcast = 1 << 10,
    MPMediaTypeMusicVideo   = 1 << 11,
    MPMediaTypeVideoITunesU = 1 << 12,
    MPMediaTypeAnyVideo     = 0xff00,

    MPMediaTypeAny          = ~0
};
typedef NSInteger MPMediaType;

回答1:


To retrieve all the music tracks from the music library, try:

MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInteger:MPMediaTypeMusic] forProperty:MPMediaItemPropertyMediaType];

MPMediaQuery *query = [[MPMediaQuery alloc] init];
[query addFilterPredicate:predicate];

NSArray *items = [query items];

To retrieve music, audio books and podcasts:

MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue [NSNumber numberWithInteger:MPMediaTypeMusic | MPMediaTypePodcast | MPMediaTypeAudioBook] forProperty:MPMediaItemPropertyMediaType comparisonType:MPMediaPredicateComparisonContains];



回答2:


I found that an MPMediaQuery predicate on MPMediaTypeAnyAudio was not enough. So I instead ran the following check on the media type and it is the best way to prevent video content from coming through.

    NSInteger mediaType = [[aMediaItem valueForProperty:MPMediaItemPropertyMediaType] intValue];
    if (mediaType <= MPMediaTypeAnyAudio) { 
        return TRUE;
    }

I found the actual values from the enum of media types had these values which let me use this simple integer comparison.

MPMediaTypeMusic: 1
MPMediaTypePodcast: 2
MPMediaTypeAudioBook: 4
MPMediaTypeAudioITunesU: 8 (iOS 5)
MPMediaTypeAnyAudio: 255
MPMediaTypeMovie: 256
MPMediaTypeTVShow: 512
MPMediaTypeVideoPodcast: 1024
MPMediaTypeMusicVideo: 2048
MPMediaTypeVideoITunesU: 4096
MPMediaTypeAnyVideo: 65280


来源:https://stackoverflow.com/questions/8420609/how-do-i-check-an-mpmediaitem-for-mpmediatype-of-just-audio

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