iPhone AVAudioRecorder 进入后台后暂停问题

发布于 2024-11-19 08:00:06 字数 1069 浏览 1 评论 0原文

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

  1. 开始录制:

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

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

    [_recorder 暂停];
    
  5. 现在,当我点击应用程序图标时,将应用程序带到前台,立即连续录制,无需采取任何操作。这更奇怪,因为检查属性:

    _recorder.recording == YES
    

返回 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 一直在增加,并且正在录制音频。

知道如何解决这个问题吗?

I have a problem with pausing AVAudioRecorder after entering background. Basically, it goes like this (_recorder is my AVAudioRecorder instance):

  1. Start recording:

    [_recorder record];
    
  2. Press the home button on the device.

  3. Because I've set observer for notification of this case (in viewDidLoad), my custom method applicationDidEnterBackground fires.
  4. In applicationDidEnterBackground I do:

    [_recorder pause];
    
  5. Now, when I tap application icon, to take app to foreground, recording continous immediately, without taking any action. It's even stranger, because checking property:

    _recorder.recording == YES
    

returns NO.

I have a function, that is called by NSTimer every 0.1s (to refresh UI etc.), and in that function I have this:

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

It prints on the console:

    ...
    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
    ...

So, as you can see, the recorder's currentTime increases all the time, and the audio is being recorded.

Any Idea how to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生活了然无味 2024-11-26 08:00:06

您应该将以下代码添加

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

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

You should add the following code:

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

to the Info.plist. This will cause the background mode to be responsive for managing audio session, so you can manually pause and resume recording.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文