views:

894

answers:

2

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?

Thanks.

+1  A: 

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

David Maymudes
Yeah, that allows you to sort the songs that match a query by playlist, but you still need the initial query, and once you've got the songs from the query you have no way of checking the playlist later (the data member you get from a search, MPMediaItem, is unaware of its own playlist). So the best this could help is if you knew the first and last song in your playlist, then you could take everything in between - but that's assuming the order you retrieve songs with is always the same.
Eli
David Maymudes
That's pretty good, thanks. I ended up just hacking my own.
Eli
A: 

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.

Eli