Question

I want to play a specific playlist (that was constructed as an iMix) from my program, as long as it exists. I am able to use

[[MPMediaQuery albumsQuery] addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:@"MyAlbum" forProperty:MPMediaItemPropertyAlbumTitle]]; 

to get all songs in an album (as well as many other options for artists, etc.) but there seems to be no way to access playlists.

Is there a different way to do this, or will I be forced to store all the songs in the playlist within my code and access them all that way?

Was it helpful?

Solution 2

I ended up having to roll my own via a text file that contains playlist information. here is the code. The [Globals split] function just takes a string and splits it into an array of strings using either a single character ([Globals split: with:]) or each character in a string ([Globals split: withMany:]).

//Create the music player for our application.
musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
[musicPlayer setShuffleMode: MPMusicShuffleModeOff];
[musicPlayer setRepeatMode: MPMusicRepeatModeAll];

//Get our song list from the text file.
NSError *error = nil;
NSString *songList = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Playlist" ofType:@"txt"] encoding:NSUTF8StringEncoding error:&error];

//Split it into each song using newlines or carriage returns.
NSArray *allSongs = [Globals split:songList withMany:@"\r\n"];
NSMutableArray *music = [NSMutableArray arrayWithCapacity:[allSongs count]];

for (int i = 0; i < [allSongs count]; i++)
{
    //Split the line into tab-delimited info: title, artist, album.
    NSArray *songInfo = [Globals split:[allSongs objectAtIndex:i] with:'\t'];

    //Get a query using all the data we have. This should return one song.
    MPMediaQuery *songQuery = [MPMediaQuery songsQuery];
    if ([songInfo count] > 0)
    {
        [songQuery addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:[songInfo objectAtIndex:0] forProperty:MPMediaItemPropertyTitle]];
    }
    if ([songInfo count] > 1)
    {
        [songQuery addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:[songInfo objectAtIndex:1] forProperty:MPMediaItemPropertyArtist]];
    }
    if ([songInfo count] > 2)
    {
        [songQuery addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:[songInfo objectAtIndex:2] forProperty:MPMediaItemPropertyAlbumTitle]];
    }

    //Add the song to our collection if we were able to find it.
    NSArray *matching = [songQuery items];
    if ([matching count] > 0)
    {
        [music addObject:[matching objectAtIndex:0]];
        printf("Added in: %s\n",[(NSString *)[(MPMediaItem *)[matching objectAtIndex:0] valueForProperty:MPMediaItemPropertyTitle] UTF8String]);
    }
    else
    {
        printf("Couldn't add in: %s\n",[(NSString *)[songInfo objectAtIndex:0] UTF8String]);
    }

}

//Now that we have a collection, make our playlist.
if  ([music count] > 0)
{
    itunesLoaded = YES;

    // just get the first album with this name (there should only be one)
    MPMediaItemCollection *itunesAlbum = [MPMediaItemCollection collectionWithItems:music];

    //Shuffle our songs.
    musicPlayer.shuffleMode = MPMusicShuffleModeSongs;

    [musicPlayer setQueueWithItemCollection: itunesAlbum];
}

The text file is very easily generated using iTunes. All you need to do is create your playlist in iTunes, remove all the song info from your list except for Title, Artist, and Album, select all, and then paste into a text file. It will automatically be tab-delimitted and split by carriage returns. You also won't need to worry about mistyping or anything like that.

OTHER TIPS

I haven't used it myself, but I see a [MPMediaQuery playlistsQuery] and MPMediaGroupingPlaylist in the docs...

Does this link help?

http://discussions.apple.com/thread.jspa?threadID=2084104&tstart=0&messageID=9838244

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top