audio recording in background on iOS

纵饮孤独 提交于 2019-12-19 11:46:18

问题


I have an application that uses location services in the background. Works fine. But what I want to do, at a certain point, is to check some audio levels while the application is in the background. For some reason it doesn't seem to work. When running in foreground, the function works fine, but when in the back ground, the audio recorder average and peak input is always -120..

This is what I use to do the trick (that doesn't seem to work apparently..)

            ....
            [recorder record];
            if (levelTimer == nil) {
                    bgTask = 0;
                    app = [UIApplication sharedApplication];
                    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
                        [app endBackgroundTask:bgTask];
                    }];
                levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.02 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
            }
            ...

And yes, the levelTimerCallback is called every 0.02 seconds, even when in the background, so I assume that the beginBackgroundTaskWithExpirationHandler works fine.

Any thoughts or some hints?


回答1:


Currently, the only supported way to record in the background on an iOS device is to include audio for the UIBackgroundModes key in the app's plist, enable an appropriate Audio Session for recording, and start recording (or playing) audio before the app goes into the background.

If the app uses the Audio Queues or the RemoteIO Audio Unit API for recording, the buffer callbacks can just throw away all the audio buffers until the time of interest, and then compute the energy represented by the PCM samples for the amount of time required when needed.




回答2:


I dont think iPhone will allow recording in the background. Only few services are allowed when the app goes into background. Logically if your app stays into background for a long time it would then consume a lot of battery as well as buffer to store audio, unless you have time and memory managed these resources. This would also pose issues for your app when you submit it to the App Store.

Although you can refer to this link for knowing what can be run in the background. I hope this might help u clear things in a way.

iPhone : Running services in the background

Good Luck!!!




回答3:


There is an app I know of that has background recording. Beatmaker 2. You need to turn run in the background on in the settings of device under beatmaker 2. In beatmaker create an audio track and start recording. Them use the home button on device to exit app but its still recording. You can get creative with different interfaces as well. I do this to feed the iPhone 4 line out into an audio track in beatmaker 2. You can record the audio of anything playing in the foreground. (YouTube videos apps music from your iTunes library. You can edit the waves also after recorded.



来源:https://stackoverflow.com/questions/12534095/audio-recording-in-background-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!