AVAudioRecorder stops working when writing to /dev/null on iPhone 5s with iOS 7

雨燕双飞 提交于 2019-12-03 03:51:00

I had the same issue when testing one of my apps for iOS 7.

I got around the absence of /dev/null by creating an initial file that I then deleted a number of seconds after the recording started. The recording still continued to work for my application and there was no data file being stored.

- (void)startRecording
{
  // New recording path.
  NSString *recorderFilePath = [NSString stringWithFormat:@"%@/%@.caf", [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"], @"cache"];
  NSURL *url = [NSURL fileURLWithPath:recorderFilePath];

  AVAudioRecorder recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];

  if([recorder prepareToRecord])
  {
    [recorder record];
    NSTimer deleteFileTimer = [NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(removeRecordingFile) userInfo:nil repeats:NO];
  }
}

- (void)removeRecordingFile
{
  // Remove the data file from the recording.
  NSString *recorderFilePath = [NSString stringWithFormat:@"%@/%@.caf", [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"], @"cache"];
  NSFileManager *fileManager = [NSFileManager defaultManager];
  NSError *error = nil;

  BOOL success = [fileManager removeItemAtPath:recorderFilePath error:&error];

  if(success) 
  {
      NSLog(@"Deleted recording file");
  }
  else
  {
      NSLog(@"Could not delete file -:%@ ",[error localizedDescription]);
  }
}

You need to change:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:NULL];

to:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:NULL];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!