iPhone AVAudioRecorder 进入后台后暂停问题
我在进入后台后暂停 AVAudioRecorder 时遇到问题。基本上,它是这样的(_recorder 是我的 AVAudioRecorder 实例):
开始录制:
[_recorder记录];
按设备上的主页按钮。
- 因为我已经设置了观察者来通知这种情况(在 viewDidLoad 中),所以我的自定义方法 applicationDidEnterBackground 会触发。
在 applicationDidEnterBackground 我做:
[_recorder 暂停];
现在,当我点击应用程序图标时,将应用程序带到前台,立即连续录制,无需采取任何操作。这更奇怪,因为检查属性:
_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):
Start recording:
[_recorder record];
Press the home button on the device.
- Because I've set observer for notification of this case (in viewDidLoad), my custom method applicationDidEnterBackground fires.
In applicationDidEnterBackground I do:
[_recorder pause];
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将以下代码添加
到 Info.plist。这将导致后台模式响应管理音频会话,因此您可以手动暂停和恢复录制。
You should add the following code:
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.