0

我遇到了麻烦

NSInvalidArgumentException 尝试使用 AudioFileCreateWithURL 时出现 nil 字符串参数

问题是,fileURL 打印NSLog正确。我相信这是正确的格式:

文件://localhost/Users/me/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/1A617C0D-90CC-465C-9108-ECBCF85F075C/Documents/recording.wav

我猜 __bridge 转换可能会导致问题。

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString* docDir = [paths objectAtIndex:0];
    NSString* file = [docDir stringByAppendingString:@"/recording.wav"];
    NSURL *fileURL = [NSURL fileURLWithPath:file];
    NSLog(@"%@", fileURL);
    OSStatus audioErr = noErr;
    audioErr = AudioFileCreateWithURL( (__bridge CFURLRef)fileURL, kAudioFileWAVEType, &asbd, kAudioFileFlags_EraseFile, &audioFile);

我正在使用 ARC。我也试过这个:

audioErr = AudioFileCreateWithURL( (CFURLRef)CFBridgingRetain(fileURL), kAudioFileWAVEType, &asbd, kAudioFileFlags_EraseFile, &audioFile);

它导致了同样的异常。

4

1 回答 1

2

你看过http://lists.apple.com/archives/coreaudio-api/2010/Aug/msg00210.html吗?他们过去常常CFStringRef让它工作。

// We have to encode spaces before sending it to CFURLCreateWithString. See CFURLCreateWithString returns NULL article http://markmail.org/message/5ugub2dvdaausdvf
    CFStringRef fileName = (CFStringRef) recordedFile; 
    CFStringRef fileNameEscaped = CFURLCreateStringByAddingPercentEscapes(NULL, fileName, NULL, NULL, kCFStringEncodingUTF8); 

    // Create the URL
    self.url = "" (CFStringRef)fileNameEscaped, NULL);    
    CHECK_NIL(url);

    // Create audio file
    OSStatus status = AudioFileCreateWithURL(url, kAudioFileWAVEType, &format, kAudioFileFlags_EraseFile, &audioFile);
    NSAssert(status == noErr, @"AudioFileCreateWithURL fails");
于 2012-12-12T20:29:18.803 回答