为什么从MPMediaPicker(iOS)返回(null)

时间:2013-01-21 20:58:00

标签: ios objective-c

我正在编程以显示iPhone音乐库,用户选择一首歌曲,然后这首歌曲标题会反映在UILabel中。如果只是那么简单!我已尝试将MPMediaItem变为NSString,然后将UILabel反映出来,但我刚刚收到(null)!

- (IBAction)showMediaPicker:(id)sender {
    MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeAny];

    mediaPicker.delegate = self;
    //mediaPicker.allowsPickingMultipleItems = YES;
    mediaPicker.prompt = @"Select Your Favourite Song!";

    [self presentModalViewController:mediaPicker animated:YES];
}

- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItem *) mediaItemCollection {

    NSString *titleString = [mediaItemCollection valueForProperty:MPMediaItemPropertyTitle];
    titleLabel.text = [NSString stringWithFormat:@"Title: %@",titleString];


    [self dismissModalViewControllerAnimated: YES];
}

提前致谢

1 个答案:

答案 0 :(得分:2)

您正在尝试获取mediaItemCollection的标题。你需要得到这样的单曲:

MPMediaItem *selectedSong = [mediaItemCollection items] objectAtIndex:0];
NSString *titleString = [selectedSong valueForProperty:MPMediaItemPropertyTitle];

顺便说一句,你的MPMediaPickerControllerDelegate设置了错误的委托:

应该是:

- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection

- 您将MPMediaItemCollection仅作为MPMediaItem