我正在编程以显示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];
}
提前致谢
答案 0 :(得分:2)
您正在尝试获取mediaItemCollection的标题。你需要得到这样的单曲:
MPMediaItem *selectedSong = [mediaItemCollection items] objectAtIndex:0];
NSString *titleString = [selectedSong valueForProperty:MPMediaItemPropertyTitle];
顺便说一句,你的MPMediaPickerControllerDelegate设置了错误的委托:
应该是:
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
- 您将MPMediaItemCollection仅作为MPMediaItem