1

我想从我的程序中播放特定的播放列表(构建为 iMix),只要它存在。我可以使用

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

获取专辑中的所有歌曲(以及艺术家的许多其他选项等),但似乎无法访问播放列表。

有没有其他方法可以做到这一点,或者我是否会被迫将所有歌曲存储在我的代码中的播放列表中并以这种方式访问​​它们?

4

2 回答 2

2

我自己没有使用过它,但我在文档中看到了一个[MPMediaQuery playlistsQuery]and ......MPMediaGroupingPlaylist

这个链接有帮助吗?

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

于 2009-12-04T01:49:13.303 回答
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 中创建播放列表,从列表中删除除标题、艺术家和专辑之外的所有歌曲信息,全选,然后粘贴到文本文件中。它将自动以制表符分隔并由回车符分割。您也不必担心输入错误或类似的事情。

于 2009-12-04T18:31:33.050 回答