0

我正在尝试使用 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];

为什么会这样?

4

3 回答 3

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");
}
于 2013-10-10T05:00:01.597 回答
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;
于 2013-10-10T03:49:50.630 回答
0

AVAudioRecorder 在文档目录或临时目录的顶层录制音频。如果您尝试在文档目录中创建的文件夹中录制音频,则会失败。始终在临时目录中录制音频,并在录制后将文件移动到 Document 目录中的文件夹中。

于 2014-12-02T09:22:07.957 回答