【问题标题】:AVAudioRecorder will not record in special casesAVAudioRecorder 在特殊情况下不会录音
【发布时间】:2013-10-10 03:39:16
【问题描述】:

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

为什么会这样?

【问题讨论】:

    标签: ios avfoundation avaudiorecorder


    【解决方案1】:

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

    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;
    

    【讨论】:

      【解决方案2】:

      试试这个

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-07
          • 1970-01-01
          • 1970-01-01
          • 2017-12-11
          • 1970-01-01
          相关资源
          最近更新 更多