简体   繁体   English

iOS中的背景录音

[英]background audio recording in iOS

I have searched far and wide for documentation on how to record audio in the background and have come to the conclusion that specifying 'audio' in the plist file might work. 我到处搜索有关如何在后台录制音频的文档,得出的结论是,在plist文件中指定“音频”可能有效。 But, because iOS 4 will terminate background apps when it runs low on memory, we must also take some steps to reduce our memory usage when we transition to the background. 但是,由于iOS 4在内存不足时会终止后台应用程序,因此当过渡到后台时,我们还必须采取一些措施来减少内存使用量。 How do we reduce our memory usage? 我们如何减少内存使用量?

Also, does anybody know a sure shot way of recording audio in the background on iOS?? 另外,有人知道在iOS上后台录制音频的可靠方法吗?

I unchecked the box in the Info.plist file that says "Application does not run in background" and also added the 我取消选中了Info.plist文件中的框,该框显示“应用程序不在后台运行”,并且还添加了

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

in Info.plist . Info.plist But, the recording stops as soon as I press the "HOME" button. 但是,只要按下“ HOME”按钮,录音就会停止。

What callbacks do we implement to know that application has gone to background? 我们执行哪些回调才能知道该应用程序已进入后台? Please advise. 请指教。

Just in case anyone else is looking for an answer here, I got mine working by adding the UIBackgroundModes array to the plist, adding 'audio' as Item 0. 万一其他人在这里寻找答案,我可以通过将UIBackgroundModes数组添加到plist并将“ audio”添加为Item 0来使我的工作正常。

I free up all memory/controllers on exit as you would by quitting the app anyhow so all that is left are the buffers the app uses (I've allocated around 1Mb though which makes me a little nervous however it seems to have worked!) I guess reducing the fidelity would help too but it seems to work as is. 我可以通过退出该应用程序来释放所有的内存/控制器,就像退出该应用程序一样,剩下的就是该应用程序使用的缓冲区(我分配了大约1Mb的内存,虽然这让我有些紧张,但是似乎已经起作用了!)我想降低保真度也会有所帮助,但它似乎可以正常工作。

In my core Audio setup I had to either change the buffer size from 1024 to 4096 or explicitly set the buffer size... I opted for the latter as latency was an issue. 在我的核心音频设置中,我不得不将缓冲区大小从1024更改为4096,或者显式设置缓冲区大小...由于延迟是一个问题,因此我选择了后者。

NSTimeInterval iobuffersize = (float)1024.0f/SAMPLE_RATE);
sizeofdata = sizeof(iobuffersize);
AudioSessionSetProperty(kAudioSessionProperty_PreferredHardwareIOBufferDuration, &sizeofdata, &iobuffersize);

I also had to make sure that it didn't kill the app on exit via not enabling the 'Does not run in background' option however this should be off by default anyhow. 我还必须确保通过不启用“不在后台运行”选项不会在退出时杀死应用程序,但是无论如何应默认将其关闭。

So guess I'm answering this for peace of mind for anyone else that it does work with not much setup after all. 因此,我猜我在回答这个问题时会让其他任何人都省心,因为它毕竟不需要太多设置。

I am however experiencing problems with Bluetooth setup, I guess that's because the buffer sizes change again but can't figure this one out... just get -50 = invalid property warning when rendering the data via the recordingCallback. 但是我在蓝牙设置时遇到问题,我想那是因为缓冲区大小再次更改,但无法弄清楚这一点...通过recordingCallback渲染数据时,得到-50 =无效属性警告。 I'm guessing it's the freq/sample size but who knows... will look later but seems like background now works. 我猜这是频率/样本大小,但谁知道...稍后再看,但似乎背景现在可以工作了。

Apart from specifying background recording in the plist file, we can implement applicationDidEnterBackground wihch will tell us when the application enters background. 除了在plist文件中指定后台记录之外,我们还可以实现applicationDidEnterBackground,它将在应用程序进入后台时告诉我们。 Here, we should stop any updates to the UI because that consumes memory for eg, updating a timer and an equalizer. 在这里,我们应该停止对UI的任何更新,因为这会消耗内存,例如,更新计时器和均衡器。

The call applicationWillEnterForeground will be called just before the app returns to foreground so we can resume whatever we stopped. 调用applicationWillEnterForeground会在应用程序返回到前台之前被调用,因此我们可以恢复停止的任何内容。

The recording then takes place in the background. 录制然后在后台进行。 It would also help to implement an interruption listener(this would work in the backgroud as well) so that you don't lose your recordings. 这也将有助于实现一个中断侦听器(这也将在后台运行),这样您就不会丢失录制内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM