iPhone 播放特定播放列表/iMix

发布于 2024-08-13 13:59:41 字数 337 浏览 7 评论 0原文

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

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心房的律动 2024-08-20 13:59:41

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

此链接有帮助吗?

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

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

揽月 2024-08-20 13:59:41

我最终不得不通过包含播放列表信息的文本文件来推出自己的。这是代码。 [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 中创建播放列表,从列表中删除除标题、艺术家和专辑之外的所有歌曲信息,选择全部,然后粘贴到文本文件中。它将自动以制表符分隔并用回车符分隔。您也无需担心输入错误或类似的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文