如何在iPhone应用程序中播放iPod库中的歌曲?

时间:2011-12-21 11:56:46

标签: iphone ipod

我对iPhone和ios开发中的音频播放非常陌生。我想在我的应用程序中播放iPod库中的歌曲。我在谷歌搜索了我的水平。但是,我得到了使用iPod库的答案,但我不知道从iPod库中播放选定的歌曲。这是我在app中使用的代码,

 MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];

        picker.delegate = self;
        picker.allowsPickingMultipleItems = NO;    
        picker.prompt = NSLocalizedString (@"Select songs to play", "Prompt in media item picker");
        [self presentModalViewController:picker animated:YES];
        [picker release];

       - (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection 
    {
        [self dismissModalViewControllerAnimated: YES];
        NSLog(@"MediaItemCollection : %@", mediaItemCollection);

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

有人可以建议我播放ipod库中的歌曲吗?

1 个答案:

答案 0 :(得分:4)

- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
    MPMusicPlayerController *player = [MPMusicPlayerController iPodMusicPlayer];
    [player setQueueWithItemCollection:mediaItemCollection];
    [player play];
}