[NSURL initFileURLWithPath:]:带有ARC的nil字符串参数

时间:2012-12-12 20:04:17

标签: ios

我遇到了

的问题
  尝试使用时,

NSInvalidArgumentException nil字符串参数   AudioFileCreateWithURL

问题是,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);

它引起了同样的例外。

1 个答案:

答案 0 :(得分: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");