我正试图通过使用以下代码从iPhone的麦克风获取音频输入:
@property (nonatomic) AudioUnit rioUnit;
@property (nonatomic) CAStreamBasicDescription outputCASBD;
(...)
// set our required format - LPCM non-interleaved 32 bit floating point
CAStreamBasicDescription outFormat = CAStreamBasicDescription(44100, // sample rate
kAudioFormatLinearPCM, // format id
4, // bytes per packet
1, // frames per packet
4, // bytes per frame
1, // channels per frame
32, // bits per channel
kAudioFormatFlagIsFloat | kAudioFormatFlagIsNonInterleaved);
try {
// Open the output unit
AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_RemoteIO;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
AudioComponent comp = AudioComponentFindNext(NULL, &desc);
AudioComponentInstanceNew(comp, &_rioUnit);
UInt32 one = 1;
XThrowIfError(AudioUnitSetProperty(_rioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &one, sizeof(one)), "couldn't enable input on the remote I/O unit");
AURenderCallbackStruct callbackStruct;
callbackStruct.inputProc = renderInput;
callbackStruct.inputProcRefCon = (__bridge void*)self;
XThrowIfError(AudioUnitSetProperty(_rioUnit, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &callbackStruct, sizeof(callbackStruct)), "couldn't set remote i/o render callback");
XThrowIfError(AudioUnitSetProperty(_rioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &outFormat, sizeof(outFormat)), "couldn't set the remote I/O unit's output client format");
XThrowIfError(AudioUnitSetProperty(_rioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &outFormat, sizeof(outFormat)), "couldn't set the remote I/O unit's input client format");
XThrowIfError(AudioUnitInitialize(_rioUnit), "couldn't initialize the remote I/O unit");
XThrowIfError(AudioOutputUnitStart(_rioUnit), "couldn't start the remote I/O unit");
}
catch (CAXException &e) {
char buf[256];
fprintf(stderr, "Error: %s (%s)\n", e.mOperation, e.FormatError(buf));
}
catch (...) {
fprintf(stderr, "An unknown error occurred\n");
}
// Allocate our own buffers (1 channel, 16 bits per sample, thus 16 bits per frame, thus 2 bytes per frame).
// Practice learns the buffers used contain 512 frames, if this changes it will be fixed in processAudio.
// will need float for accellerate fft (input is 16 bit signed integer) so make space...
_tempBuffer.mNumberChannels = outFormat.mChannelsPerFrame;
_tempBuffer.mDataByteSize = 512 * outFormat.mBytesPerFrame * 2;
_tempBuffer.mData = malloc( self.tempBuffer.mDataByteSize );
一切都很好,直到我想停止听麦克风输入。
我这样做:
AudioOutputUnitStop(_rioUnit);
AudioUnitUninitialize(_rioUnit);
AudioComponentInstanceDispose(_rioUnit);
_rioUnit = nil;
但是我在为kAudioOutputUnitProperty_SetInputCallback
定义的回调函数中收到错误。
错误是:AURemoteIO::IOThread (14): EXC_BAD_ACCESS (code=1, address=0x8000000c)
在我看来,AudioUnit的InputCallback仍然被调用,即使我正在停止它,对其实例进行单元化和处置。
为什么会发生这种情况?如何阻止在我停止,整理和处理其实例后调用InputCallback?
答案 0 :(得分:2)
首先,确保_rioUnit在停止时确实具有与开始时相同的值。
在另一个异步线程中调用音频输入回调。因此,在未初始化或处置音频单元或任何其他所需的数据结构之前,您可能需要等待该线程也停止(通常在少于2个音频缓冲持续时间内发生,因此这可能是等待时间的合理时间)
答案 1 :(得分:2)
问题在于这一行:
_rioUnit = nil; // causes EXC_BAD_ACCESS error
将nil
更改为NULL
解决了这个问题:
_rioUnit = NULL; // works smoothly
我从这篇文章中得到了这个想法: http://teragonaudio.com/article/How-to-do-realtime-recording-with-effect-processing-on-iOS.html
此外,this answer概述了nil
中NULL
与Objective-C
之间的差异。