在 iOS 后台录制分块音频
我正在尝试找出一种在 iOS 应用程序后台录制音频并将其传输到服务器的方法。
当应用程序位于前台时,我几乎可以正常工作。我使用 AVAudioRecorder 记录输入 X 秒。一旦我收到已完成的回调,我就会再录制 X 秒。每个记录会话都存储到不同的文件中,我将这些文件异步发送到服务器。
但是,当我的应用程序进入后台模式时,这似乎不起作用。
进入后台时,当前记录会话会继续记录,直到 X 秒结束,但是在我可以开始另一个记录会话之前,我的应用程序会被暂停。
有什么想法吗?
这是我的回调代码:
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)aRecorder successfully:(BOOL)flag {
NSLog(@"hello");
[self initRecorder];
[recorder recordForDuration:5];
}
I am trying to figure out a way to record audio in the background of an iOS application and have it streamed to the server.
I have pretty much got this working for when the app is in the foreground. I use AVAudioRecorder to record input for X seconds. Once I get the callback that this has finished, I record for another X seconds. Each recording session gets stored to a different file and I send these files asynchronously to the server.
However, this doesn't seem to work while my app goes into background mode.
When going into the background, the current record session continues recording until the X seconds are up, however my app gets suspended, before I can start another recording session.
Any ideas?
Here's the code for my callback:
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)aRecorder successfully:(BOOL)flag {
NSLog(@"hello");
[self initRecorder];
[recorder recordForDuration:5];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法在后台重新开始录制。
因此,请改用音频队列或音频单元 RemoteIO API,这将为您提供更小的音频“块”(回调缓冲区块),而无需停止音频录制。
如果您的网络协议需要,可将小型音频回调块连接成较大的文件块。
You can't restart recording in the background.
So use the Audio Queue or Audio Unit RemoteIO APIs instead, which will give you smaller "chunks" (callback buffer blocks) of audio without stopping the audio recording.
Concatenate small audio callback chunks into larger file chunks if needed for your network protocol.
多任务处理支持后台音频播放,但后台音频录制是否支持还不是很清楚。不过,我还没有尝试过。音频单元 API 可能允许您在应用程序处于后台时继续录制音频。然而,这是一种伎俩,我想它可能会在某个时候被取消。
Background audio playing is supported with multitasking but it's not very clear that background audio recording is. However, I have not tried it. The Audio Unit API might let you continue to record audio while the application is in the background. However, this is kind of a trick and I Imagine it might get pulled out at some point.