如何从库中获取音乐文件

时间:2012-06-19 12:38:00

标签: iphone

我开发了一个应用程序。我需要从手机库中获取音乐文件。对于那个使用MPMediaPickerCOntroller.But它不会触发didpickingitem委托方法。我的代码如下所示。

- (void)viewDidLoad
 {
[super viewDidLoad];
MPMediaPickerController *picker =[[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];
picker.delegate                     = self;
picker.allowsPickingMultipleItems   = YES;
picker.prompt                       =@"Add songs to play";
[self presentModalViewController:picker animated: YES];
}

 - (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection 
   {
        NSLog(@"sdfadsf");
        NSLog(@"%@",mediaItemCollection);
        NSArray *slist=[mediaItemCollection copy];
         NSLog(@"%@",slist);

  }

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您是否在.h?

中声明了MPMediaPickerControllerDelegate

例如:

  

@interface FirstViewController:UIViewController   

如果不这样可以解释为什么该方法没有触发。另外,我不相信它会在模拟器上运行;只在设备上。

编辑:

-(IBAction)presentLibrary:(id)sender
{
    //this is called from a button press, but you could do it in viewDidLoad
    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];    
}

- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
     NSLog(@"didpick");
    if (mediaItemCollection) {
        [_mediaPlayer setQueueWithItemCollection: mediaItemCollection];
        _mediaCollection = mediaItemCollection;
    }

    [mediaPicker dismissModalViewControllerAnimated:YES];
}