进入后台后iPhone AVAudioRecorder暂停问题

Mar*_*cin 2 iphone audio background recording

进入后台后,我有一个暂停AVAudioRecorder的问题.基本上,它是这样的(_recorder是我的AVAudioRecorder实例):

  1. 开始录制:

    [_recorder record];
    
    Run Code Online (Sandbox Code Playgroud)
  2. 按设备上的主页按钮.

  3. 因为我已经为此案例的通知设置了观察者(在viewDidLoad中),我的自定义方法applicationDidEnterBackground会触发.
  4. 在applicationDidEnterBackground我做:

    [_recorder pause];
    
    Run Code Online (Sandbox Code Playgroud)
  5. 现在,当我点击应用程序图标时,将应用程序带到前台,立即录制,不采取任何操作.这甚至更奇怪,因为检查财产:

    _recorder.recording == YES
    
    Run Code Online (Sandbox Code Playgroud)

返回NO.

我有一个函数,NSTimer每0.1秒调用一次(刷新UI等),在这个函数中我有这个:

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

它在控制台上打印:

    ...
    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
    ...
Run Code Online (Sandbox Code Playgroud)

因此,正如您所看到的,录音机的currentTime一直在增加,并且正在录制音频.

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

kon*_*owy 7

您应该添加以下代码:

<key>UIBackgroundModes</key>
<array>
    <string>audio</string>
</array>
Run Code Online (Sandbox Code Playgroud)

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