Michael のコードを動作させるのに少し問題があります。以下に私の実装を示します。iOS 6.1 で実行しようとしていますが、次のエラーが発生していますEXC_BAD_ACCESS
。
AudioBuffer バッファ = bufferList.mBuffers[i];
appdelegate
次の場合、ファイルから以下のコードを実行していますapplicationfinishloading
。
_rec = [[AU 割り当て]init];
[_rec initializeAudio];
[_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