在iOS中附加来自图库的声音文件

时间:2012-06-21 11:40:40

标签: iphone ios xcode

是否有可能像我的iOS应用中的图片库一样访问声音文件库,我尝试在网上搜索但没有找到任何关于它的好内容。

2 个答案:

答案 0 :(得分:1)

您可以参考Apple的AddMusic示例应用程序,看看它是如何完成的......

答案 1 :(得分:0)

是的,您可以将其上传到服务器。使用以下代码:

从选择器中选择一首歌

- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection 
{
    MPMediaItem *song = nil ;
    [self dismissModalViewControllerAnimated:YES];

    if ([mediaItemCollection count] < 1) 
    {
        return;
    }
    [song release];
    song = [[[mediaItemCollection items] objectAtIndex:0] retain];

    //song.accessibilityHin

    [self uploadMusicFile:song];
}

- (void) uploadMusicFile:(MPMediaItem *)song
{
    // Init audio with playback capability
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];

    NSURL *assetURL = [song valueForProperty:MPMediaItemPropertyAssetURL];
    AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
 // to get uniqe time interval
    [[NSDate date] timeIntervalSince1970];
    // convert this ti string
    NSTimeInterval seconds = [[NSDate date] timeIntervalSince1970];
    NSString *intervalSeconds = [NSString stringWithFormat:@"%0.0f",seconds];

    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset: songAsset                                                                       presetName: AVAssetExportPresetPassthrough];

    exporter.outputFileType = @"public.mpeg-4";

    NSString *exportFile = [[NSString alloc] initWithString:[DOCUMENTS_FOLDER stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",intervalSeconds]]];


    NSURL *exportURL = [[NSURL fileURLWithPath:exportFile] retain];
    exporter.outputURL = exportURL; 

    [exporter exportAsynchronouslyWithCompletionHandler:
     ^{
         NSData *data = [NSData dataWithContentsOfURL:exportURL];

         // Here your upload code 
     }];
}