使用iOS 7在iPhone 5s上写入/ dev / null时,AVAudioRecorder停止工作 [英] AVAudioRecorder stops working when writing to /dev/null on iPhone 5s with iOS 7

查看:183
本文介绍了使用iOS 7在iPhone 5s上写入/ dev / null时,AVAudioRecorder停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序可以监控背景音频级别而无需录制到文件。我使用写入/ dev / null的技巧来实现这一目标。

I have an app that monitors the background audio level without recording to a file. I use the trick of writing to /dev/null to accomplish this.

此代码适用于带有iOS 6的iPhone 3GS,带有iOS 6和iOS 7的iPhone 4 ,以及iOS 7和iPhone Retina(4英寸64位)的模拟器。

This code has worked on the iPhone 3GS with iOS 6, iPhone 4 with iOS 6 and iOS 7, and in the simulator with iOS 7 and iPhone Retina (4-inch 64-bit).

然而,当我在真正的iPhone 5s上试用它时,录像机似乎暂时捕捉音频,然后默默地死去。

When I try it on a real iPhone 5s, however, the recorder seems to capture audio for a moment, and then silently dies.

这是代码:

    // Inititalize the audio

NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];

NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithFloat: 44100.0],                 AVSampleRateKey,
                          [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
                          [NSNumber numberWithInt: 1],                         AVNumberOfChannelsKey,
                          [NSNumber numberWithInt: AVAudioQualityMax],         AVEncoderAudioQualityKey,
                          nil];


NSError *error;

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

if (recorder) {
    [recorder prepareToRecord];
    recorder.meteringEnabled = YES;
    [recorder record];
    levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
    //        peakLevelTimer = [NSTimer scheduledTimerWithTimeInterval: 2.0 target: self selector: @selector(peakLevelTimerCallback:) userInfo: nil repeats: YES];
} else {
    NSLog(@"There was an error setting up the recorder.");        
    NSLog([error description]);
}

任何想法可能会发生什么?

Any ideas what might be going on?

有人可以建议解决方法吗?写入真实文件有效,但我不想填充设备上的任何空间只是为了监听音频。有没有办法写入一个只是蒸发成稀薄空气的小文件缓冲区?有效地实现我自己的/ dev / null?

Could anyone suggest a workaround? Writing to a real file works, but I don't want to fill up any space on the device just to monitor audio. Is there a way to write to a small file buffer that just evaporates into thin air? Implement my own /dev/null, effectively?

推荐答案

在测试我的iOS 7应用程序时遇到了同样的问题。

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

我通过创建一个初始文件来解决缺少/ dev / null的问题,然后在录制开始后删除了几秒钟。录音仍然继续适用于我的应用程序,并且没有存储数据文件。

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]);
  }
}

这篇关于使用iOS 7在iPhone 5s上写入/ dev / null时,AVAudioRecorder停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆