使用AudioQueueGetProperty检索记录格式的常规参数错误

时间:2012-09-26 22:10:14

标签: iphone ios core-audio

我从调用AudioQueueGetProperty获得-50(一般参数错误)。请帮助我,因为我已经触及XCode和任何iPhone工作几个月了。这可能是我的一个简单的蠢事,但我无法解决它。我的代码导致-50:

//Setup format
AudioStreamBasicDescription recordFormat;
memset(&recordFormat, 0, sizeof(recordFormat));
recordFormat.mFormatID = kAudioFormatMPEG4AAC;
recordFormat.mChannelsPerFrame = 2;
CCGetDefaultInputDeviceSampleRate(&recordFormat.mSampleRate);
UInt32 propSize = sizeof(recordFormat);
AQ(AudioFormatGetProperty(kAudioFormatProperty_FormatInfo, 0, NULL,
                                  &propSize, &recordFormat), 
           "AudioFormatGetProperty throws unexpected errors.");

//Setup Queue
//listing 4.8-4.9
AudioQueueRef theQueue = {0};
self->queue = theQueue;
AQ(AudioQueueNewInput(&recordFormat, CCAudioRecordingCallback, 
                              self, NULL, NULL, 0, &self->queue),
           "AudioQueueNewInput throws unexpected errors.");
UInt32 size = sizeof(recordFormat);
AQ(AudioQueueGetProperty(self->queue,
                                 kAudioConverterCurrentOutputStreamDescription, 
                                 &recordFormat, 
                                 &size), 
           "Getting audio property kAudioConverterCurrentOutputStreamDescription throws unexpected errors.");

我已经确认我有一个有效的队列,就像我调用AudioQueueGetProperty一样。我已经尝试了两种方式传递队列“self-> queue”和“self.queue”,它们都会导致相同的错误。队列定义如下:

@interface CCAudioRecorder()
//...
@property (nonatomic, assign) AudioQueueRef queue;
//...

@end

@implementation CCAudioRecorder
@synthesize queue;

AQ是#def:

#define AQ(expr, msg) if(nil!=CheckError((expr), msg)) [NSException raise:@"AudioException" format:@"Unexpected exception occured."];

解决了以下错误检查功能:

static NSString* CheckError(OSStatus error, const char* operation)
{
    if (noErr == error) return nil;

    NSString *errorMessage = nil;
    char errorString[20];
    //See if it appears to be a 4-char code
    *(UInt32 *)(errorString+1) = CFSwapInt32HostToBig(error);
    if ( isprint(errorString[1]) && isprint(errorString[2]) 
        && isprint(errorString[3]) && isprint(errorString[4]) ) 
    {
        errorString[0] = errorString[5] = '\'';
        errorString[6] = '\0';
    } else {
        sprintf(errorString, "%d", (int)error);
    }
    errorMessage = [NSString stringWithFormat:
                    @"Audio Error: %@ (%@)\n", 
                    [NSString stringWithUTF8String:operation], 
                    [NSString stringWithUTF8String:errorString]];
    NSLog(@"%@", errorMessage);
    return errorMessage;
}

我也尝试在使用局部变量创建的队列上调用AudioFormatGetProperty,避免类实例级别iVar并仍然得到相同的错误:

AudioQueueRef theQueue = {0};
AQ(AudioQueueNewInput(&recordFormat, CCAudioRecordingCallback, 
                              self, NULL, NULL, 0, &theQueue),
           "AudioQueueNewInput throws unexpected errors.");
UInt32 size = sizeof(recordFormat);
AQ(AudioQueueGetProperty(theQueue,
                                 kAudioConverterCurrentOutputStreamDescription, 
                                 &recordFormat, 
                                 &size), 
           "Getting audio property kAudioConverterCurrentOutputStreamDescription throws unexpected errors.");

**更新** 我有以下代码可以在模拟器上工作,而不是在设备上。 (我没有把它与我之前发布的内容交叉引用,但我相信它或者相似或完全相同。)

AudioQueueRef theQueue = {0};
self->queue = theQueue;
AQ(AudioQueueNewInput(&recordFormat, CCAudioRecordingCallback, 
                              self, NULL, NULL, 0, &self->queue),
           "AudioQueueNewInput throws unexpected errors.");
UInt32 size = sizeof(recordFormat);
AQ(AudioQueueGetProperty(self->queue, 
                                 kAudioConverterCurrentOutputStreamDescription, 
                                 &recordFormat, 
                                 &size), 
           "Getting audio property kAudioConverterCurrentOutputStreamDescription throws unexpected errors.");

在设备上运行我在同一位置发生崩溃并出现错误-50一般参数错误。我的设备是运行iOS6的iPhone 4S我正在使用XCode 4.5。

1 个答案:

答案 0 :(得分:1)

您正在关注learning core audio图书中的代码吗?第4章关于实现录像机?我注意到你的代码和书的代码之间存在差异,在书中他们只是初始化队列并按原样使用它:

AudioQueueRef queue = {0};
UInt32 size = sizeof(recordFormat);
CheckError(AudioQueueGetProperty(queue, kAudioConverterCurrentOutputStreamDescription,
                                 &recordFormat, &size), "couldn't get queue's format");

我不确定你为什么要混淆self。但这肯定是造成你的错误的原因。如果全部失败,只需下载该章here的完整代码,并查看您可以识别错误的位置。