我在让迈克尔的代码工作时遇到了一些麻烦。下面你将看到我的实现,我试图让它在 iOS 6.1 上运行,但我得到了一个EXC_BAD_ACCESS
:
AudioBuffer 缓冲区 = bufferList.mBuffers[i];
我正在从appdelegate
文件中运行以下代码applicationfinishloading
:
_rec = [[AU alloc]init];
[_rec 初始化音频];
[_rec 开始];
提前致谢.. :)
#define kOutputBus 0
#define kInputBus 1
@implementation AU
AudioComponentInstance audioUnit;
AudioStreamBasicDescription audioFormat;
float *convertedSampleBuffer;
-(id)init{
self = [super init];
if (self){
}
return self;
}
-(OSStatus)start{
NSLog(@"start");
OSStatus status = AudioOutputUnitStart(audioUnit);
return status;
}
-(OSStatus)stop{
OSStatus status = AudioOutputUnitStop(audioUnit);
return status;
}
-(void)cleanUp{
AudioUnitUninitialize(audioUnit);
}
static OSStatus recordingCallback(void *inRefCon,
AudioUnitRenderActionFlags *actionFlags,
const AudioTimeStamp *audioTimeStamp,
UInt32 inBusNumber,
UInt32 numFrames,
AudioBufferList *buffers) {
AudioBufferList bufferList;
for (int i = 0; i < bufferList.mNumberBuffers; i++)
{
AudioBuffer buffer = bufferList.mBuffers[i];
SInt16 *audioFrame = (SInt16*)buffer.mData;
}
return noErr;
}
-(void)initializeAudio{
NSLog(@"init");
OSStatus status;
AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_RemoteIO;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
// Get component
AudioComponent inputComponent = AudioComponentFindNext(NULL, &desc);
// Get audio units
status = AudioComponentInstanceNew(inputComponent, &audioUnit);
// Enable IO for recording
UInt32 flag = 1;
status = AudioUnitSetProperty(audioUnit,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Input,
kInputBus,
&flag,
sizeof(flag));
// Describe format
audioFormat.mSampleRate = 44100.00;
audioFormat.mFormatID = kAudioFormatLinearPCM;
audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
audioFormat.mFramesPerPacket = 1;
audioFormat.mChannelsPerFrame = 1;
audioFormat.mBitsPerChannel = 16;
audioFormat.mBytesPerPacket = 2;
audioFormat.mBytesPerFrame = 2;
// Apply format
status = AudioUnitSetProperty(audioUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output,
kInputBus,
&audioFormat,
sizeof(audioFormat));
// Set input callback
AURenderCallbackStruct callbackStruct;
callbackStruct.inputProc = recordingCallback;
callbackStruct.inputProcRefCon = (__bridge void *)(self);
status = AudioUnitSetProperty(audioUnit,
kAudioOutputUnitProperty_SetInputCallback,
kAudioUnitScope_Global,
kInputBus,
&callbackStruct,
sizeof(callbackStruct));
status = AudioUnitInitialize(audioUnit);
}
@end