如何在iphone中播放itunes库中的歌曲

时间:2009-11-03 03:45:29

标签: iphone ipod

嗨我需要播放来自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];

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

谢谢, 世斌。

3 个答案:

答案 0 :(得分: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]; 
}

现在您需要实现委托以将歌曲存储到本地变量中。在此,selectedSongCollectionMPMediaItemCollection的实例。

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

完成选择歌曲后,请执行委托以解除选择器:

- (void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker 
{   
    [self dismissModalViewControllerAnimated: YES]; 
}

答案 1 :(得分:2)

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

答案 2 :(得分: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]