录音时如何将音频素材实时上传到服务器?基本上我的要求是录制时将卡片/数据包上传到音频剪辑.
我已经使用IQAudioRecorderController https://github.com/hackiftekhar/IQAudioRecorderController做了录音部分.它记录音频并保存到TemporaryDirectory.
我已经使用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.您必须将其替换为您自己的上传到服务器的逻辑.请注意,虽然您可以轻松地做到这一点,但一个问题是这将给您原始的音频样本.如果您需要它们作为波形文件或类似的,您可能需要等到录制完成,因为文件的标题需要有关包含的音频样本的信息.