iOS:尝试获取audioframe值时的EXC_BAD_ACCESS

时间:2013-03-08 05:12:19

标签: ios ios6 core-audio audiounit

让Michael的code工作有点麻烦。下面你将看到我的实现,我试图让它在iOS 6.1上运行,但我得到EXC_BAD_ACCESS at:

  

AudioBuffer buffer = bufferList.mBuffers [i];

我在appdelegate

时运行applicationfinishloading文件中的以下代码
  

_rec = [[AU alloc] init];

     

[_ rec initializeAudio];

     

[_ rec start];

提前致谢.. :))

#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

1 个答案:

答案 0 :(得分:0)

您需要先调用AudioUnitRender来获取音频数据(从您链接的页面中获取代码)

AudioUnitRender([audioInterface audioUnit], 
                             ioActionFlags, 
                             inTimeStamp, 
                             inBusNumber, 
                             inNumberFrames, 
                             bufferList);

请注意,在某些情况下,您可能还需要使用malloc自己的bufferList来使用。