繁体   English   中英

AVAudioRecorder在特殊情况下将不会录制

[英]AVAudioRecorder will not record in special cases

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

为什么会这样呢?

使用此代码来检查您的目录是否正确创建。

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;

尝试这个

-(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");
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM