iphoneipadios5caf

Cannot create URL in application support directory for caf file


When I write this code it work will

    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);

but when I change recordFile to be such that

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];

I see the message AudioFileCreateWithURL failed in the console, what's wrong in my code, am sure that the path I use in the second code exists , and work , inRecordFile is the caf file name


Solution

  •     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;
        }
    

    Why are you going in more complexity? just when ur sound is recorded, copy caf file to your desired directory.