除了 RemoteIO 本身的设置过程(相当艰巨但由Apple 的示例代码更好地涵盖)之外,洞察的关键点是:
- 使用与我最初设置流相同的
AudioStreamBasicDescription
( )。*audioFormat
根据其他问题和帖子,我不知道我花了多长时间尝试设置一个参数略有不同的新参数。只需从我的 ivar 中引用流属性就足够了。
- 设置一个“isRecording”布尔值,这样您就可以打开和关闭写入文件,而无需拆除并重新设置您的 RemoteIO 会话
- 在,嗯,回调中写入文件是可以的
recordingCallback
,但是异步执行。很多信息都在谈论在playbackCallback 中执行此操作或设置第三个audioFileWriteCallback。这导致了无声文件或 4KB(即空)文件。不要这样做。
- 此外,请务必使用传递给回调的 ioData的副本
在recordingCallback
之后AudioUnitRender
进入bufferList
:
AudioDeviceManager* THIS = (__bridge AudioDeviceManager *)inRefCon;
if (THIS->isRecording) {
ExtAudioFileWriteAsync(THIS->extAudioFileRef, inNumberFrames, bufferList);
}
- 开始和停止录音功能,供参考:
-(void)startRecording {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *destinationFilePath = [documentsDirectory stringByAppendingPathComponent:kAudioFileName];
CFURLRef destinationURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)destinationFilePath, kCFURLPOSIXPathStyle, false);
OSStatus status;
// create the capture file
status = ExtAudioFileCreateWithURL(destinationURL, kAudioFileWAVEType, &audioFormat, NULL, kAudioFileFlags_EraseFile, &extAudioFileRef);
if (status) NSLog(@"Error creating file with URL: %ld", status);
// use the same "audioFormat" AudioStreamBasicDescription we used to set up RemoteIO in the first place
status = ExtAudioFileSetProperty(extAudioFileRef, kExtAudioFileProperty_ClientDataFormat, sizeof(AudioStreamBasicDescription), &audioFormat);
ExtAudioFileSeek(extAudioFileRef, 0);
ExtAudioFileWrite(extAudioFileRef, 0, NULL);
isRecording = YES;
}
- (void)stopRecording {
isRecording = NO;
OSStatus status = ExtAudioFileDispose(extAudioFileRef);
if (status) printf("ExtAudioFileDispose %ld \n", status);
}
就是这样!