进入后台后iPhone AVAudioRecorder暂停问题

时间:2011-07-07 16:31:26

标签: iphone audio background recording

输入背景后我有一个暂停AVAudioRecorder的问题。基本上,它是这样的(_recorder是我的AVAudioRecorder实例):

  1. 开始录制:

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

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

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

    _recorder.recording == YES
    
  6. 返回NO。

    我有一个函数,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
        ...
    

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

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

1 个答案:

答案 0 :(得分:7)

您应该添加以下代码:

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

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

相关问题