我正在尝试使用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];
为什么会这样?
答案 0 :(得分:0)
使用此代码检查您的目录是否正确创建。
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat: @"yyyy-MM-dd ss"];
NSString *dateWithString = [formatter stringFromDate:date];
NSLog(@"%@",dateWithString);
docsDir = [[dirPaths objectAtIndex:0] stringByAppendingPathComponent:dateWithString];
NSError *e;
if (![[NSFileManager defaultManager] fileExistsAtPath:docsDir]) //Does directory already exist?
{
if (![[NSFileManager defaultManager] createDirectoryAtPath:docsDir
withIntermediateDirectories:NO
attributes:nil
error:&e])
{
NSLog(@"Create directory error: %@", e);
}
}
在申请之后,
NSString *soundFilePath = [docsDir
stringByAppendingPathComponent:@"sound.caf"];
NSLog(@"%@",soundFilePath);
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath isDirectory:NO];
NSDictionary *recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil];
NSError *error2;
答案 1 :(得分:0)
试试这个
-(IBAction)record:(id)sender
{
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat: @"yyyy-MM-dd ss"];
NSString *dateWithString = [formatter stringFromDate:date];
NSLog(@"%@",dateWithString);
NSString *dateFolderPath = [docsDir stringByAppendingPathComponent:dateWithString];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:dateFolderPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dateFolderPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
NSString *soundFilePath = [dateFolderPath
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;
recorder = [[AVAudioRecorder alloc]
initWithURL:soundFileURL
settings:recordSettings
error:&error2];
[recorder record];
}
-(IBAction)stop:(id)sender
{
[recorder stop];
NSLog(@"stop");
}
答案 2 :(得分:0)
AVAudioRecorder将音频记录在文档目录或临时目录中的顶层。如果您尝试在文档目录中创建的文件夹中录制音频,则它将失败。始终在临时目录中录制音频,并在将文件移动到文件目录中的文件夹后记录。