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

sma*_*str 7 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];
Run Code Online (Sandbox Code Playgroud)

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

谢谢,Shibin.

Vai*_*idu 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]; 
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

- (void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker 
{   
    [self dismissModalViewControllerAnimated: YES]; 
}
Run Code Online (Sandbox Code Playgroud)