我需要將輸出採樣率從44.1更改爲32.0,但它總是會拋出一個錯誤Out:AudioUnitSetProperty-SF = \ 217 \ 325 \ 377 \ 377,-10865。我不知道爲什麼它會讓我將其設置爲輸入,但不會將其設置爲輸出。CoreAudio AudioUnitSetProperty總是無法設置採樣率
我的代碼是:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
OSStatus MyRenderer(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData){
NSLog(@"Running...");
ioData->mBuffers[0].mDataByteSize = 2048;
ioData->mBuffers[0].mData = lbuf;
ioData->mBuffers[0].mNumberChannels = 1;
return noErr;
}
void CreateDefaultAU(){
OSStatus err = noErr;
// Open the default output unit
AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_DefaultOutput;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
desc.componentManufacturer = 0;
AudioComponent comp = AudioComponentFindNext(NULL, &desc);
if (comp == NULL) { printf ("FindNextComponent\n"); return; }
err = AudioComponentInstanceNew(comp, &gOutputUnit);
if (comp == NULL) { printf ("OpenAComponent=%ld\n", err); return; }
// Set up a callback function to generate output to the output unit
AURenderCallbackStruct input;
input.inputProc = MyRenderer;
input.inputProcRefCon = NULL;
err = AudioUnitSetProperty(gOutputUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &input, sizeof(input));
if (err) { printf ("AudioUnitSetProperty-CB=%ld\n", err); return; }
AudioStreamBasicDescription streamFormat;
streamFormat.mSampleRate = 32000.00; // the sample rate of the audio stream
streamFormat.mFormatID = kAudioFormatLinearPCM; // the specific encoding type of audio stream
streamFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger;//kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsNonMixable;
streamFormat.mFramesPerPacket = 1;
streamFormat.mChannelsPerFrame = 1;
streamFormat.mBitsPerChannel = 16;
streamFormat.mBytesPerPacket = 2;
streamFormat.mBytesPerFrame = 2;
err = AudioUnitSetProperty(gOutputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &streamFormat, sizeof(streamFormat));
if (err) { printf ("In: AudioUnitSetProperty-SF=%4.4s, %ld\n", (char*)&err, err); return; }
err = AudioUnitSetProperty(gOutputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &streamFormat, sizeof(streamFormat));
if (err) { printf ("Out: AudioUnitSetProperty-SF=%4.4s, %ld\n", (char*)&err, err); return; }
}
void TestDefaultAU(){
OSStatus err = noErr;
// Initialize unit
err = AudioUnitInitialize(gOutputUnit);
if (err) { printf ("AudioUnitInitialize=%ld\n", err); return; }
Float64 outSampleRate;
UInt32 size = sizeof(Float64);
err = AudioUnitGetProperty(gOutputUnit, kAudioUnitProperty_SampleRate, kAudioUnitScope_Output, 0, &outSampleRate, &size);
printf("Out srate %f\n",outSampleRate);
if (err) { printf ("AudioUnitSetProperty-GF=%4.4s, %ld\n", (char*)&err, err); return; }
AudioOutputUnitStart (gOutputUnit);
if (err) { printf ("AudioOutputUnitStart=%ld\n", err); return; }
AudioUnitReset (gOutputUnit, kAudioUnitScope_Input, 0);
}
我添加了你的代碼,它給了我這個新的輸出: 2010-01-31 09:33:59.342播放器[86903:80f]大小:66144 2010-01-31 09:33:59.345播放器[86903 :80f]採樣率:32000 2010-01-31 09:33:59.645播放器[86903:80f] deviceName =內置輸出 2010-01-31 09:33:59.646播放器[86903:80f]採樣率: 44100.000000 2010-01-31 09:33:59.647播放器[86903:80f] isWritable true 2010-01-31 09:33:59.648播放器[86903:80f] Set NominalSampleRate err 2010-01-31 09:34: 00.030播放器[86903:80f]輸出採樣率現在爲44100.000000 Hz –
是否刪除了輸出範圍的SetProperty調用? – VoidPointer