iPhone AVAudioRecorder进入后台后暂停问题

问题描述:

我在进入后台后暂停AVAudioRecorder时出现问题。基本上,它是这样的(_recorder是我AVAudioRecorder实例):iPhone AVAudioRecorder进入后台后暂停问题

  1. 开始录音:

    [_recorder record]; 
    
  2. 按下设备上的主页按钮。

  3. 因为我已经设置了观察者通知这种情况(在viewDidLoad),我的自定义方法applicationDidEnterBackground触发。
  4. 在applicationDidEnterBackground我做的:

    [_recorder pause]; 
    
  5. 现在,当我点击应用程序图标,采取应用前景,立即记录连续,没有采取任何行动。它甚至陌生人,因为检查属性:

    _recorder.recording == YES 
    

;否。

我有一个功能,即通过所谓的NSTimer每隔0.1秒(刷新UI等),并在该函数我有这样的:

if(_recorder.recording) { 
    NSLog(@"recording"); 
} 
else { 
    NSLog(@"not recording"); 
    NSLog(@"time: %f", _recorder.currentTime); 
} 

它打印在控制台上:

... 
    not recording 
    time: 9.404082 
    not recording 
    not recording 
    time: 9.473197 
    not recording 
    time: 9.531179 
    not recording 
    time: 9.629206 
    not recording 
    time: 9.728435 
    ... 

因此,如您所见,录音机的当前时间一直增加,并且正在录制音频。

任何想法如何解决这个问题?

您应该添加以下代码:

<key>UIBackgroundModes</key> 
<array> 
    <string>audio</string> 
</array> 

到Info.plist中。这将导致后台模式响应管理音频会话,因此您可以手动暂停并恢复录制。