我从对 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。