Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

core-audio

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;
like image 973
Brennan Avatar asked Dec 07 '11 18:12

Brennan


2 Answers

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];
like image 200
sbooth Avatar answered Sep 23 '22 21:09

sbooth


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
like image 44
Brennan Avatar answered Sep 25 '22 21:09

Brennan