我正在录制一些音频,当我使用此代码选择存储音频数据的目录时没问题:
NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsFolder = [folders objectAtIndex:0];
result = [documentsFolder stringByAppendingPathComponent:@"Recording.m4a"];
但我需要将音频文件存储在另一个目录中,然后我尝试了这段代码:
NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsFolder = [folders objectAtIndex:0];
result = [documentsFolder stringByAppendingPathComponent:@"Sounds/Recording.m4a"];
AVAudioRecorder 实例使用新路径初始化,但是当我调用它[myAudioRecorer prepareToRecord]
时[myAudioRecorder record]
它失败并且记录了任何声音。
如何正确更改目录?
PS:我也尝试用 NSFileManager 创建一个新目录,但是没有用。我是这样做的:
[[NSFileManager defaultManager] createDirectoryAtPath:pathAsString withIntermediateDirectories:YES attributes:nil error:&error]
编辑:
这是我录制音频的代码:
//here is the method that returns the path i want to save the files
- (NSString *) audioRecordingPath{
NSString *result = nil;
NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsFolder = [folders objectAtIndex:0];
result = [documentsFolder stringByAppendingPathComponent:@"Sounds/Recording.m4a"];
return result;
}
//just the record settings
- (NSDictionary *) audioRecordingSettings{
NSDictionary *result = nil;
NSMutableDictionary *settings = [[NSMutableDictionary alloc] init];
[settings setValue:[NSNumber numberWithInteger:kAudioFormatAppleLossless] forKey:AVFormatIDKey];
[settings setValue:[NSNumber numberWithFloat:44100.0f] forKey:AVSampleRateKey];
[settings setValue:[NSNumber numberWithInteger:1] forKey:AVNumberOfChannelsKey];
[settings setValue:[NSNumber numberWithInteger:AVAudioQualityLow] forKey:AVEncoderAudioQualityKey];
result = [NSDictionary dictionaryWithDictionary:settings];
return result;
}
//here is where i record the audio
- (IBAction)recordAudio{
NSError *error = nil;
NSString *pathAsString = [self audioRecordingPath];
NSURL *audioRecordingURL = [NSURL fileURLWithPath:pathAsString];
self.audioRecorder = [[AVAudioRecorder alloc] initWithURL:audioRecordingURL settings:[self audioRecordingSettings] error:&error];
if (self.audioRecorder != nil){
self.audioRecorder.delegate = self;
if ([self.audioRecorder prepareToRecord] && [self.audioRecorder record]){
NSLog(@"Successfully started to record.");
} else {
NSLog(@"Failed to record.");// the error returned.
self.audioRecorder = nil;
}
} else {
NSLog(@"Failed to create an instance of the audio recorder.");
}
}