RemoteIO 录制的音频文件为静音或 4KB
Posted
技术标签:
【中文标题】RemoteIO 录制的音频文件为静音或 4KB【英文标题】:RemoteIO recorded audio file is either silent or 4KB 【发布时间】:2012-11-21 20:45:30 【问题描述】:我正在成功使用 RemoteIO 对来自麦克风的传入音频流执行分析。不过,我似乎无法将文件写入磁盘。我已经阅读了一些问题:
Example of saving audio from RemoteIO?, AudioBufferList contents in remoteIO audio unit playback callback, Recording to AAC from RemoteIO: data is getting written but file unplayable Recording from RemoteIO: resulting .caf is pitch shifted slower + distorted并尝试在那里实施建议:除非它们不起作用。调用 ExtAudioFileWriteAsync 的正确位置在哪里,如何设置?
【问题讨论】:
【参考方案1】:除了 RemoteIO 本身的设置过程(相当艰巨但由Apple's example code 更好地涵盖)之外,洞察的关键点是:
-
使用与我最初设置流时相同的
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);
就是这样!
【讨论】:
以上是关于RemoteIO 录制的音频文件为静音或 4KB的主要内容,如果未能解决你的问题,请参考以下文章