7

嗨,我需要播放 iTunes 库中的歌曲。我浏览了 Apples ipod Library Access Guide 并获得了代码。

MPMediaQuery *everything = [[MPMediaQuery alloc] init];
NSLog(@"Logging items from a generic query...");
NSArray *itemsFromGenericQuery = [everything items];
MPMediaItem *song;
for (song in itemsFromGenericQuery) 
{
    NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
    NSLog (@"%@", songTitle);
}

//assign a playback queue containing all media items on the device
[myPlayer setQueueWithQuery:everything];//setQueueWithQuery:everything];

//start playing from the begining
[myPlayer play];

但这将从库列表的最开始播放。当我从列表中选择一首歌时,我需要播放它。任何人都可以帮助我吗...

谢谢,世斌。

4

3 回答 3

6

使用该MPMediaPickerController实例,您可以从 iPod 库的歌曲列表、专辑列表等中进行选择。这是一个从 iPod 中选择所有歌曲并显示在模式视图控制器中的示例。

- (IBAction) selectSong: (id) sender 
{   
    MPMediaPickerController *picker =
    [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];

    picker.delegate                     = self;
    picker.allowsPickingMultipleItems   = NO;
    picker.prompt                       = NSLocalizedString (@"Select any song from the list", @"Prompt to user to choose some songs to play");

    [self presentModalViewController: picker animated: YES];
    [picker release]; 
}

现在您需要实现委托以将歌曲存储到本地变量中。这里,selectedSongCollection是 的一个实例MPMediaItemCollection

- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection 
{
    [self dismissModalViewControllerAnimated: YES];
    selectedSongCollection=mediaItemCollection; 
}

完成歌曲选择后,执行委托以关闭选择器:

- (void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker 
{   
    [self dismissModalViewControllerAnimated: YES]; 
}
于 2009-11-03T05:08:23.790 回答
2

您正在将所有歌曲的播放列表分配给音乐播放器,因此它当然会从头开始播放整个列表。如果您希望用户从 iPod 库中选择一首特定歌曲,请使用MPMediaPickerController.

于 2009-11-03T03:48:53.983 回答
2

我无法MPMediaPickerController在我的场景中使用。

我对问题的简短回答是看看[musicplayer setNowPlayingItem:item]

下面是我的实现中的一些代码。

// Create a new query
MPMediaQuery *query = [MPMediaQuery songsQuery];
MPMediaPropertyPredicate *mpp = [MPMediaPropertyPredicate predicateWithValue:@"a" forProperty:MPMediaItemPropertyTitle comparisonType:MPMediaPredicateComparisonContains];
[query addFilterPredicate:mpp]; 

// Retrieve the results and reload the table data
DATAENV.songCollections = [NSMutableArray arrayWithArray:query.collections];

//populate cell rows with 

- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MPMediaItem *item = [[[DATAENV.songCollections objectAtIndex:indexPath.row] items] lastObject];
    titleLbl = [item valueForProperty:MPMediaItemPropertyTitle];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MPMediaItem *item = [[[self.songCollections objectAtIndex:indexPath.row] items] lastObject];
    [PLAYER setNowPlayingItem:item];
    [PLAYER play];
}

PLAYER/DATAENV 是我的单身人士

#define PLAYER  [[AudioController sharedAudioController_instance] musicPlayer]
#define DATAENV [DataEnvironment sharedDataEnvironment_instance]
于 2010-12-30T03:29:53.727 回答