2012-02-21 99 views
0

創建應用程序的支持目錄URL當我寫這篇文章的代碼它的工作將無法爲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文件名

+0

您每次都從頭開始覆蓋'Dpath'值。這是你真正想要的嗎? – Costique 2012-02-21 09:49:39

+0

是的我想獲得完整的路徑,在這個目錄中創建caf文件的問題 – AMH 2012-02-21 09:51:04

回答

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文件複製到你想要的目錄。