我想从我的程序中播放特定的播放列表(构建为iMix),只要它存在即可。我可以使用
[[MPMediaQuery albumsQuery] addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:@"MyAlbum" forProperty:MPMediaItemPropertyAlbumTitle]];
获取相册中的所有歌曲(以及艺术家的许多其他选项等),但似乎无法访问播放列表。
是否有不同的方法可以执行此操作,或者我是否会被迫在我的代码中存储播放列表中的所有歌曲并以这种方式访问它们?
答案 0 :(得分:2)
我自己没有使用它,但我在文档中看到了[MPMediaQuery playlistsQuery]
和MPMediaGroupingPlaylist
...
此链接有帮助吗?
http://discussions.apple.com/thread.jspa?threadID=2084104&tstart=0&messageID=9838244
答案 1 :(得分:1)
我最终不得不通过包含播放列表信息的文本文件滚动自己。这是代码。 [Globals split]函数只接受一个字符串,并使用单个字符([Globals split:with:])或字符串中的每个字符([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];
}
使用iTunes可以轻松生成文本文件。您需要做的就是在iTunes中创建播放列表,从列表中删除除标题,艺术家和专辑之外的所有歌曲信息,选择全部,然后粘贴到文本文件中。它将自动进行制表符分隔并通过回车分割。您也不必担心错误输入或类似的事情。