iOS iPod API - 获取实际的歌曲文件

时间:2013-02-20 20:36:54

标签: ios api file ipod

在我的应用程序中,我用魔杖呈现媒体选择器,让用户选择1首歌曲,然后获取它的实际MP3(或任何其他格式,只要其可由其他iOS设备播放)文件。

如何实现? 如何使用歌曲文件实际创建对象?

1 个答案:

答案 0 :(得分:6)

我不知道为什么每个人都在说这么多关于从应用商店被拒绝的事情。我将简要介绍如何开始。

- 显示媒体选择器以打开用户的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

}