简体   繁体   中英

Upload audio clip realtime while its recording?

How to upload audio clip realtime to a server while its recording? Basically my requirement is upload an audio clip as chucks/packets while its recording. I already did the recording part with using IQAudioRecorderController https://github.com/hackiftekhar/IQAudioRecorderController . It records the audio and save to TemporaryDirectory.

I wanted to know how to upload realtime without saving the audio clip.

This is the recording part

//Unique recording URL


NSString *fileName = [[NSProcessInfo processInfo] globallyUniqueString];
_recordingFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.m4a",fileName]];


// Initiate and prepare the recorder

_audioRecorder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:_recordingFilePath] settings:recordSetting error:nil];
_audioRecorder.delegate = self;
_audioRecorder.meteringEnabled = YES;


// Recording start
- (void)recordingButtonAction:(UIBarButtonItem *)item
{
 if (_isRecording == NO)
  {
    _isRecording = YES;

    //UI Update
    {
        [self showNavigationButton:NO];
        _recordButton.tintColor = _recordingTintColor;
        _playButton.enabled = NO;
        _trashButton.enabled = NO;
    }

    /*
     Create the recorder
     */
    if ([[NSFileManager defaultManager] fileExistsAtPath:_recordingFilePath])
    {
        [[NSFileManager defaultManager] removeItemAtPath:_recordingFilePath error:nil];
    }

    _oldSessionCategory = [[AVAudioSession sharedInstance] category];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];
    [_audioRecorder prepareToRecord];
    [_audioRecorder record];
}
else
{
    _isRecording = NO;

    //UI Update
    {
        [self showNavigationButton:YES];
        _recordButton.tintColor = _normalTintColor;
        _playButton.enabled = YES;
        _trashButton.enabled = YES;
    }

    [_audioRecorder stop];
    [[AVAudioSession sharedInstance] setCategory:_oldSessionCategory error:nil];
}
}  

// Recording done

-(void)doneAction:(UIBarButtonItem*)item
{
  if ([self.delegate respondsToSelector:@selector(audioRecorderController:didFinishWithAudioAtPath:)])
  {
      IQAudioRecorderController *controller = (IQAudioRecorderController*)[self navigationController];
      [self.delegate audioRecorderController:controller didFinishWithAudioAtPath:_recordingFilePath];
  }

  [self dismissViewControllerAnimated:YES completion:nil];
}

There are various ways of solving this, one way is to create your own AudioGraph. The AudioGraph can grab samples from microphone or from a file. Then you proceed to an output unit, but install a callback to get the sampled frames. These you then push to your network class which then can upload packet by packet.

A good example that shows you how to write these captured packets to disk is AVCaptureAudioDataOutput .

In that example packets are written suing ExtAudioFileWriteAsync. You have to replace this with your own logic for uploading to a server. Note that while you can do that easily, one problem is that this will give you raw audio samples. If you need them as wave file or similar, you may need to wait until recording is finished, since the header of the file needs an information about contained audio samples.

The code you are currently using will work for you if you want to upload recorded file after recording is done as it will give you the final recorded file.

If you want to upload live audio recording to the server then I think you have to go with combination of,

AudioSession for recording stuff

ffmpeg for uploading your live audio to server.

You can get good help for recording audio and managing Audio Buffers from here

For ffmpeg I think you have to lear a lot. It will be easy to send static/saved audio file to server using ffmpeg but for sending live Audio Buffer to server will be tricky job.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM