Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
}
like image 776
smartsanja Avatar asked May 05 '15 04:05

smartsanja


People also ask

Can you upload just audio to YouTube?

Only audio files and their associated metadata and album art can be uploaded. Video files, PDFs, and other types of content are not supported. YouTube Music will automatically remove duplicate copies from your library if the same content is uploaded multiple times.


1 Answers

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.

like image 164
Volker Avatar answered Oct 05 '22 06:10

Volker