如何在录制的同时将音频片段实时上传到服务器?基本上我的要求是在录制时将音频剪辑上传为夹头/数据包。 我已经使用 IQAudioRecorderController 完成了录音部分 https://github.com/hackiftekhar/IQAudioRecorderController .它记录音频并保存到 TemporaryDirectory。
我想知道如何在不保存音频剪辑的情况下实时上传。
这是录音部分
//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];
}
最佳答案
有多种方法可以解决这个问题,一种方法是创建您自己的 AudioGraph。 AudioGraph 可以从麦克风或文件中抓取样本。然后你继续一个输出单元,但是安装一个回调来获取采样帧。然后将这些推送到网络类,然后网络类可以逐个数据包上传。
向您展示如何将这些捕获的数据包写入磁盘的一个很好的例子是 AVCaptureAudioDataOutput .
在该示例中,数据包是使用 ExtAudioFileWriteAsync 编写的。您必须将其替换为您自己的上传到服务器的逻辑。请注意,虽然您可以轻松做到这一点,但一个问题是这将为您提供原始音频样本。如果您需要它们作为 wave 文件或类似文件,您可能需要等到录制完成,因为文件的标题需要有关包含的音频样本的信息。
关于ios - 录制时实时上传音频片段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30044041/