【问题标题】:AVAudioRecorder stops working when writing to /dev/null on iPhone 5s with iOS 7在使用 iOS 7 的 iPhone 5s 上写入 /dev/null 时,AVAudioRecorder 停止工作
【发布时间】:2013-10-17 03:51:52
【问题描述】:

我有一个应用程序可以监控背景音频电平而不录制到文件中。我使用写入 /dev/null 的技巧来完成此操作。

此代码已在装有 iOS 6 的 iPhone 3GS、装有 iOS 6 和 iOS 7 的 iPhone 4 以及装有 iOS 7 和 iPhone Retina(4 英寸 64 位)的模拟器上运行。

然而,当我在真正的 iPhone 5s 上试用它时,录音机似乎捕捉到了一会儿音频,然后就无声无息地死掉了。

这是代码:

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

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

有人可以提出解决方法吗?写入真实文件是可行的,但我不想仅仅为了监视音频而占用设备上的任何空间。有没有办法写入一个刚刚蒸发到空气中的小文件缓冲区?有效地实现我自己的 /dev/null 吗?

【问题讨论】:

    标签: ios7 avaudiorecorder


    【解决方案1】:

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

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

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

    【讨论】:

    • 太棒了!这是完美的。
    • 尽管您已删除文件的链接,但这仍会占用文件系统空间。这与写入 /dev/null 不同。
    • 我有完全相同的代码。我不需要停止使用 /dev/null。相反,我按照此处的说明进行操作 - stackoverflow.com/questions/2676292/… - 请注意:我没有在 iPhone5 上进行测试,如出现问题的设备所示。
    【解决方案2】:

    你需要改变:

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

    到:

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

    【讨论】:

    • 这可能是评论,但不是问题的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多