我实现了 MPMediaPickerController
- (IBAction)pickSong:(id)sender {
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");
picker.showsCloudItems = YES;
[self presentModalViewController: picker animated: YES];
}
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection
{
[self dismissModalViewControllerAnimated: YES];
if (mediaItemCollection.count > 0) {
MPMediaItem *mediaItem = [mediaItemCollection.items objectAtIndex:0];
NSLog(@"%@ - %@, %@", [mediaItem valueForProperty:MPMediaItemPropertyTitle], [mediaItem valueForProperty:MPMediaItemPropertyArtist], [mediaItem valueForProperty:MPMediaItemPropertyAssetURL]);
self.fileURL = [mediaItem valueForProperty:MPMediaItemPropertyAssetURL];
}
}
- (void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker
{
[self dismissModalViewControllerAnimated: YES];
}
我还单独实现了 UIDocumentInteractionController 以便能够在任何能够处理它的应用程序中打开一首歌曲
- (IBAction)shareButtonPressed:(id)sender {
NSURL *url = [[NSBundle mainBundle] URLForResource:@"Song.mp3" withExtension:nil];
self.docController= [UIDocumentInteractionController interactionControllerWithURL:url];
[self.docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
现在我想做的是合并他们两个,我想分享从用户那里挑选的歌曲,这就是我保存歌曲的 URL self.fileURL = [mediaItem valueForProperty:MPMediaItemPropertyAssetURL];
,我对分享方法做了一个简单的改变
- (IBAction)shareButtonPressed:(id)sender {
NSURL *url = self.fileURL;
self.docController= [UIDocumentInteractionController interactionControllerWithURL:url];
[self.docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
我认为这会起作用,但我得到的是一个错误说
* -[UIDocumentInteractionController setURL:], /SourceCache/UIKit/UIKit-2903.23/UIDocumentInteractionController.m:1010 2013-10-28 03:14:10.294 ShareTest[1567:60b] 中的断言失败 *由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序',原因:'UIDocumentInteractionController:无效的方案 ipod-library。仅支持文件方案。
问题显然出在 URL 上,我没有很好地实现它,或者我认为解决方案是获取 MPMediaItem 的 NSData 并将其保存为我的应用程序中的 .mp3 文件,但我不知道如何也实现这一目标
先感谢您