在我的应用程序中,我想呈现媒体选择器,让用户选择一首歌曲,然后获取它的实际 MP3(或任何其他格式,只要其他 iOS 设备可以播放)文件。
如何才能做到这一点? 我怎样才能用歌曲文件实际创建一个对象?
我不知道为什么每个人都在说那么多关于被应用商店拒绝的废话。我将向您简要介绍如何开始。
-显示媒体选择器以打开用户的 iPod:
MPMediaPickerController *pickerController = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
pickerController.prompt = NSLocalizedString(@"Choose Song", NULL);
pickerController.allowsPickingMultipleItems = NO;
pickerController.delegate = self;
[self presentViewController:pickerController animated:YES completion:nil];
[pickerController release];
-等待用户选择一首歌曲并在 mediaPicker 委托中获取回调:
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
MPMediaItem *theChosenSong = [[mediaItemCollection items]objectAtIndex:0];
NSString *songTitle = [theChosenSong valueForProperty:MPMediaItemPropertyTitle];
NSString *artist = [theChosenSong valueForProperty:MPMediaItemPropertyArtist];
//then just get the assetURL
NSURL *assetURL = [theChosenSong valueForProperty:MPMediaItemPropertyAssetURL];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
//Now that you have this, either just write the asset (or part of) to disk, access the asset directly, send the written asset to another device etc
}