将音频文件从文档发布到服务器

时间:2014-05-01 11:31:13

标签: ios avfoundation avaudiorecorder

您好我正在创建一个记录音频的演示应用程序。我已经按照本教程

Link to tutorial

// Set the audio file
    NSArray *pathComponents = [NSArray arrayWithObjects:
                               [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                               @"MyAudioMemo.m4a",
                               nil];
    NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

    // Setup audio session
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

    // Define the recorder setting
    NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];

    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
    [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

    // Initiate and prepare the recorder
    recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
    recorder.delegate = self;
    recorder.meteringEnabled = YES;
    [recorder prepareToRecord];

录制部分工作正常,应用程序正在播放音频文件,

问题: 如何将录制的文件发布到服务器。

1 个答案:

答案 0 :(得分:0)

您可以使用以下示例:

NSURLSession *session = [NSURLSession sharedSession];
[[session uploadTaskWithRequest:[NSURLRequest requestWithURL:your_url]
                      fromFile:file_url
             completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

             }] resume];

请在此处阅读更多http://www.raywenderlich.com/51127/nsurlsession-tutorial