gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:16:30 24 4
gpt4 key购买 nike

我正在编程让 iPhone 音乐库显示,用户选择一首歌曲,然后这首歌的标题反射(reflect)在 UILabel 中。要是这么简单就好了!我已经尝试将 MPMediaItem 放入 NSString 中,然后将 UILabel 反射(reflect)出来,但我只是得到 (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];
}

提前致谢

最佳答案

您正在尝试获取 mediaItemCollection 的标题。您需要像这样获取单独的歌曲名称:

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

另外,您的 MPMediaPickerControllerDelegate 的委托(delegate)设置有误:

应该是:

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

-您将 MPMediaItemCollection 作为 MPMediaItem

关于ios - 为什么从 MPMediaPicker (iOS) 返回 (null),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14447099/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com