IOS iPhone设置kAudioSessionProperty_PreferredHardwareIOBufferDuration似乎不工作

时间:2012-01-25 17:40:19

标签: ios audio-recording

以下是一些设置音频捕获缓冲区大小(以秒为单位)的代码

union 
{
    OSStatus propertyResult;
    char a[4];
} u;

union
{
    UInt32 UI32sessionCat;
    char a[4];
} usc;

UInt32 UI32sessionCatSize = 4;
Float32 F32realIOBufferDuration = 0.0;
Float32 F32requestedIOBufferDuration = 0.5;
UInt32 F32datasize = 4;

u.propertyResult = AudioSessionGetProperty (  kAudioSessionProperty_PreferredHardwareIOBufferDuration , &F32datasize, &F32realIOBufferDuration );
NSLog(@"Error Get IO Duration Time %ld %lx %c%c%c%c",u.propertyResult,u.propertyResult,u.a[3],u.a[2],u.a[1],u.a[0]);
NSLog(@"IO Buffer Duration is %f",F32realIOBufferDuration);

u.propertyResult = AudioSessionSetProperty (  kAudioSessionProperty_PreferredHardwareIOBufferDuration ,sizeof(F32requestedIOBufferDuration) , &F32requestedIOBufferDuration );
NSLog(@"Error Set IO Duration Time %ld %lx %c%c%c%c",u.propertyResult,u.propertyResult,u.a[3],u.a[2],u.a[1],u.a[0]);

u.propertyResult = AudioSessionGetProperty (  kAudioSessionProperty_PreferredHardwareIOBufferDuration , &F32datasize, &F32realIOBufferDuration );
NSLog(@"Error Get IO Duration Time %ld %lx %c%c%c%c",u.propertyResult,u.propertyResult,u.a[3],u.a[2],u.a[1],u.a[0]);
NSLog(@"IO Buffer Duration is %f",F32realIOBufferDuration);

u.propertyResult = AudioSessionGetProperty (  kAudioSessionProperty_AudioCategory, &UI32sessionCatSize, &usc.UI32sessionCat );
NSLog(@"Error Get Property %ld %lx %c%c%c%c",u.propertyResult,u.propertyResult,u.a[3],u.a[2],u.a[1],u.a[0]);
NSLog(@"Category %ld %lx %c%c%c%c",usc.UI32sessionCat,usc.UI32sessionCat,usc.a[3],usc.a[2],usc.a[1],usc.a[0]);

这似乎正在起作用/设定持续时间...

2012-01-25 12:28:01.275 recordonlytest[1623:707] Error Get IO Duration Time 0 0 
2012-01-25 12:28:01.276 recordonlytest[1623:707] IO Buffer Duration is 0.023000
2012-01-25 12:28:01.278 recordonlytest[1623:707] Error Set IO Duration Time 0 0 
2012-01-25 12:28:01.280 recordonlytest[1623:707] Error Get IO Duration Time 0 0 
2012-01-25 12:28:01.281 recordonlytest[1623:707] IO Buffer Duration is 0.500000
2012-01-25 12:28:01.283 recordonlytest[1623:707] Error Get Property 0 0 
2012-01-25 12:28:01.284 recordonlytest[1623:707] Category 1919247201 72656361 reca

您可以看到get属性确实返回0.50000

但回调仍然以.023000的速度发生

2012-01-25 12:28:04.035 recordonlytest[1623:707] In Capture Output
2012-01-25 12:28:04.058 recordonlytest[1623:707] In Capture Output
2012-01-25 12:28:04.081 recordonlytest[1623:707] In Capture Output
2012-01-25 12:28:04.104 recordonlytest[1623:707] In Capture Output
2012-01-25 12:28:04.128 recordonlytest[1623:707] In Capture Output
2012-01-25 12:28:04.151 recordonlytest[1623:707] In Capture Output
2012-01-25 12:28:04.174 recordonlytest[1623:707] In Capture Output
2012-01-25 12:28:04.197 recordonlytest[1623:707] In Capture Output
2012-01-25 12:28:04.220 recordonlytest[1623:707] In Capture Output
2012-01-25 12:28:04.244 recordonlytest[1623:707] In Capture Output

这已经设定

 [self getRequestedSampleRate];  // routine to set the duration

 NSError* activationError = nil;

 [[AVAudioSession sharedInstance] setActive: YES error: &activationError];

 audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio]; 
 captureSession = [[AVCaptureSession alloc] init];

那么是否需要完成不同的序列等,或者IOS是否忽略了此请求?

1 个答案:

答案 0 :(得分:1)

来自here

虽然无论会话激活状态如何,设置音频类别(kAudioSessionProperty_AudioCategory)或属性侦听器等音频会话属性都是安全的,但通常最好先发出首选请求,例如首选硬件缓冲持续时间(kAudioSessionProperty_PreferredHardwareIOBufferDuration)或首选硬件音频会话未激活时的采样率(kAudioSessionProperty_PreferredHardwareSampleRate)。

您可能错过了:

// *在询问“当前”属性* 之前激活音频会话 AudioSessionSetActive(真);