我实现了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];
}
现在我要做的是合并他们两个,我想分享从用户那里挑选的歌曲,这就是我保存歌曲的网址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文件,但我不知道我也知道如何实现这个目标
提前谢谢
答案 0 :(得分:5)
问题是您作为参数传递的URL类型。
您需要使用此方法:[NSURL fileURLWithPath:path]
为UIDocumentInteractionController
创建路径。
您创建的网址方案为ipod-library
,但必须为file