简体   繁体   English

在录音时实时上传音频片段?

[英]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 . 我已经使用IQAudioRecorderController https://github.com/hackiftekhar/IQAudioRecorderController进行了录音部分。 It records the audio and save to TemporaryDirectory. 它记录音频并保存到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. 有多种解决方法,一种方法是创建自己的AudioGraph。 The AudioGraph can grab samples from microphone or from a file. AudioGraph可以从麦克风或文件中获取样本。 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 . 向您展示如何将捕获的数据包写入磁盘的一个很好的示例是AVCaptureAudioDataOutput

In that example packets are written suing ExtAudioFileWriteAsync. 在该示例中,使用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 用于录制内容的AudioSession

ffmpeg for uploading your live audio to server. ffmpeg用于将您的现场音频上传到服务器。

You can get good help for recording audio and managing Audio Buffers from here 您可以从此处获得录制音频和管理音频缓冲区的良好帮助。

For ffmpeg I think you have to lear a lot. 对于ffmpeg,我认为您必须学习很多。 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. 使用ffmpeg将静态/保存的音频文件发送到服务器很容易,但是将实时音频缓冲区发送到服务器将是一件棘手的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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