我最後不得不通過包含播放列表信息的文本文件,以推出自己的。這裏是代碼。 [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中創建播放列表,從標題,藝術家和專輯除外的列表中移除所有歌曲信息,全選,然後粘貼到文本文件中。它將自動被製表符分隔並通過回車符分割。你也不需要擔心忘記或類似的東西。
來源
2009-12-04 18:31:33
Eli
是的,它允許你排序與播放列表查詢匹配的歌曲,但你仍然需要初始查詢,並且一旦你從查詢中獲得了歌曲,你將無法檢查播放列表(數據您從搜索中獲得的成員MPMediaItem不知道其自己的播放列表)。所以最好的解決辦法是,如果你知道播放列表中的第一首和最後一首歌,那麼你可以將所有內容放在一起 - 但假設你檢索歌曲的順序總是相同的。 – Eli 2009-12-04 15:34:11
做鏈接幫助嗎?看起來類似於你想要做的:http://discussions.apple.com/thread.jspa?threadID=2084104&tstart=0&messageID=9838244 – 2009-12-04 17:27:55
這很不錯,謝謝。我結束了自己的黑客攻擊。 – Eli 2009-12-04 18:23:47