我正在尝试使用 AVAudioRecorder 录制用户的声音。当我使用此代码时,它可以工作:
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat: @"yyyy-MM-dd ss"];
NSString *dateWithString = [formatter stringFromDate:date];
NSLog(@"%@",dateWithString);
NSString *soundFilePath1 = [docsDir
stringByAppendingPathComponent:nil];
NSString *soundFilePath = [soundFilePath1
stringByAppendingPathComponent:@"sound.caf"];
NSLog(@"%@",soundFilePath);
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSDictionary *recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil];
NSError *error2;
self.recorder = [[AVAudioRecorder alloc]
initWithURL:soundFileURL
settings:recordSettings
error:&error2];
但是,当我使用时它不起作用:
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat: @"yyyy-MM-dd ss"];
NSString *dateWithString = [formatter stringFromDate:date];
NSLog(@"%@",dateWithString);
NSString *soundFilePath1 = [docsDir
stringByAppendingPathComponent:dateWithString];
NSString *soundFilePath = [soundFilePath1
stringByAppendingPathComponent:@"sound.caf"];
NSLog(@"%@",soundFilePath);
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSDictionary *recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil];
NSError *error2;
self.recorder = [[AVAudioRecorder alloc]
initWithURL:soundFileURL
settings:recordSettings
error:&error2];
这两个代码块之间的区别是:
NSString *soundFilePath1 = [docsDir stringByAppendingPathComponent:dateWithString];
为什么会这样?