2012-05-11 143 views
0

我已經在我的應用程序中實現了音頻錄製,但我需要知道如何將音頻錄製文件保存在文檔目錄中,並在其他類中檢索用於播放音頻的相同文件。如何將iPhone音頻文件保存在文檔目錄中

這裏是源代碼。

-(void)startPushed 
{ 
     NSMutableDictionary *rs = [[NSMutableDictionary alloc] init]; 
     [rs setValue:[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey]; 
     [rs setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 
     [rs setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey]; 
     recordedTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"caf"]]]; 

     recorder = [[AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:rs error:&error]; 
     [recorder setDelegate:self]; 
     [recorder prepareToRecord]; 
     [recorder record]; 


     NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); 
     NSString *docDir = [docPath objectAtIndex:0]; 
     NSString *fullPath = [docDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", recordedTmpFile]]; 
     NSLog(@"%@", fullPath); 
} 

-(void)playbackPushed 
    { 
    AVAudioPlayer *avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error]; 
     NSLog(@"Play Path:%@", recordedTmpFile); 
     [avPlayer prepareToPlay]; 
     [avPlayer play]; 
    } 

請告訴我如何讀取和寫入文件目錄 音頻文件(格式的.caf),也給了其他類音頻播放的信息。

在此先感謝

回答

0

訪問文檔目錄在iOS中使用此代碼

-(NSURL *)documentDirectory{ 
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; 

}

和使用的NSFileManager到文件複製到該路徑。

相關問題