AudioFileCreateWithURL失败,返回-50

时间:2013-06-08 15:06:42

标签: ios core-audio

    NSString *docsDir;
    NSArray *dirPaths;
    //
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    NSString *databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"test.aif"]];
    NSURL *fileURL = [NSURL URLWithString:databasePath];
    //
    AudioStreamBasicDescription asbd;
    memset(&asbd, 0, sizeof(asbd));
    asbd.mSampleRate = 44100;
    asbd.mFormatID = kAudioFormatLinearPCM;
    asbd.mFormatFlags = kAudioFormatFlagIsBigEndian | kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
    asbd.mChannelsPerFrame = 1;
    asbd.mFramesPerPacket = 1;
    asbd.mBitsPerChannel = 16;
    asbd.mBytesPerFrame = 2;
    asbd.mBytesPerPacket = 2;
    //
    AudioFileID audioFile;
    OSStatus audioErr = noErr;
    audioErr = AudioFileCreateWithURL((CFURLRef)fileURL,
                                      kAudioFileAIFFType,
                                      &asbd,
                                      kAudioFileFlags_EraseFile,
                                      &audioFile);
    NSLog(@"%ld",audioErr);

我用iphone模拟器运行代码,日志打印是-50,我查了一下苹果的doc,但是找不到错误代码-50,我不知道“-50”是什么意思,所以有什么问题?我在哪里可以得到错误代码?谢谢。

更新:我将代码“NSURL *fileURL = [NSURL URLWithString:databasePath];”更改为“CFURLRef fileURL = ( CFURLRef)[NSURL fileURLWithPath:databasePath];”,是的,它不返回错误代码-50,并返回成功代码0,但是serval秒后,它将返回错误代码1886547263,我发现它意味着“无法打开或创建用于写入的音频文件”。

2 个答案:

答案 0 :(得分:2)

您可以使用macerror查找一些错误代码:

$ macerror -50
Mac OS error -50 (paramErr): error in user parameter list

答案 1 :(得分:0)

-50是kAudio_ParamError

我不相信你可以用你的方式将NSURL强制转换为CFURL。我是这样做的(使用你的vars):

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *pathForTheFile= [docsDir stringByAppendingPathComponent:@"test.aif"];
CFURLRef fileURL = (__bridge CFURLRef)[NSURL fileURLWithPath:pathForTheFile];

然后,在您对AudioFileCreateWithURL的调用中:

audioErr = AudioFileCreateWithURL(fileURL,
                                  kAudioFileAIFFType,
                                  &asbd,
                                  kAudioFileFlags_EraseFile,
                                  &audioFile);