无法在caf文件的应用程序支持目录中创建URL

时间:2012-02-21 09:43:57

标签: iphone ipad ios5 caf

当我编写此代码时,它将工作

    NSString *recordFile = [NSTemporaryDirectory() stringByAppendingPathComponent: (NSString*)inRecordFile];    

    NSLog(@"////////////////////////// recordFile /////////////////////");
    NSLog(recordFile) ;
    url = CFURLCreateWithString(kCFAllocatorDefault, (CFStringRef)recordFile, NULL);

    // create the audio file
    XThrowIfError(AudioFileCreateWithURL(url, kAudioFileCAFType, &mRecordFormat, kAudioFileFlags_EraseFile,
                                      &mRecordFile), "AudioFileCreateWithURL failed");
    CFRelease(url);

但是当我将recordFile更改为

NSString* Dpath ; 
NSArray * paths;

// Build the Directory 

paths  = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);

Dpath = [paths objectAtIndex:0];

Dpath = [DICOMpath stringByAppendingPathComponent:@"YYYYYYYYYYYY"];

Dpath = [DICOMpath stringByAppendingPathComponent:[FirstDirectory stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]] ;

Dpath = [DICOMpath stringByAppendingPathComponent:[SecondtDirectory stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]];

Dpath = [DICOMpath stringByAppendingPathComponent:@"XXX"];
NSString *recordFile = [Dpath stringByAppendingPathComponent:inRecordFile];

我在控制台中看到AudioFileCreateWithURL消息失败,我的代码出了什么问题,确定我在第二个代码中使用的路径存在,并且工作,inRecordFile是caf文件名

1 个答案:

答案 0 :(得分:1)

    NSError *error = nil;
    NSString *documentFolderPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *mainImageFolderPath=[documentFolderPath stringByAppendingPathComponent:@"Audio"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:mainImageFolderPath])
{
    [[NSFileManager defaultManager] createDirectoryAtPath:mainImageFolderPath
                              withIntermediateDirectories:YES
                                               attributes:nil
                                                    error:NULL];
}
    NSDateFormatter *nameFormatter = [[NSDateFormatter alloc] init];
    [nameFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString *nameString = [nameFormatter stringFromDate:[NSDate date]];
    [nameFormatter release];
    NSString *AudioPath = [mainImageFolderPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.caf",nameString]];
    [[NSFileManager defaultManager] copyItemAtPath:[NSTemporaryDirectory() stringByAppendingPathComponent: @"recordedFile.caf"] toPath:AudioPath error:&error];
    if(error)
        NSLog(@"Error audio %@", [error description]);
    else{
        [self.notifier voiceAdded:AudioPath];
        self.recordPath = AudioPath;
        containsSound = YES;
    }

为什么你会更复杂?就在录制你的声音时,将caf文件复制到你想要的目录。