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];
}
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.
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.
Related
We have a VOIP application, that records and plays audio. As such, we are using PlayAndRecord (kAudioSessionCategory_PlayAndRecord) audio session category.
So far, we have used it successfully with iPhone 4/4s/5 with both iOS 6 and iOS 7 where call audio and tones played clearly and were audible.
However, with iPhone 5s, we observed that both the call audio and tones sound robotic/garbled in speaker mode. When using earpiece/bluetooth/headset, sound is clear and audible.
iOS Version used with iPhone 5s: 7.0.4
We are using audiounits for recording/playing of call audio.
When setting audio properties like session category, audio route, session mode etc., we tried both the older (deprecated) AudioSessionSetProperty() and AVAudioSession APIs.
For playing tones, we are using AVAudioPlayer. Playing of tones during the VOIP call and also when pressing keypad controller within the app produces robotic sound.
When instantiating the audio component using AudioComponentInstanceNew, we set componentSubType to kAudioUnitSubType_VoiceProcessingIO.
When replacing kAudioUnitSubType_VoiceProcessingIO with kAudioUnitSubType_RemoteIO, we noticed that the sound of call audio and tones was no longer robotic, it was quite clear, but the volume level was very low when using speaker mode.
In summary, keeping all the other audio APIs the same:
kAudioUnitSubType_VoiceProcessingIO: Volume is high (desirable) but sound of tones and call audio was robotic in speaker mode.
kAudioUnitSubType_RemoteIO: Sound of tones and call audio was clear but it is not audible.
STEPS TO REPRODUCE
- Set audio session category to playAndRecord.
- Set audio route to speaker
- Set all the other audio properties like starting audio unit, activating the audio session, instantiating the audio components.
- Set the input and render callbacks
- Try both options
1. Play tones using AVAudioPlayer
2. Play call audio
Any suggestions on how to get over this issue. Raised as an issue with Apple but no response yet from them.
i have shared the code here github link
The only difference between kAudioUnitSubType_VoiceProcessingIO and kAudioUnitSubType_RemoteIO is that voiceProcessing includes code to tune out acoustic echo i.e. tunes out the noise from the speaker so the microphone doesn't pick it up. Its been a long time since I've played with the audio framework but I remember that to sound off there could be any number of things,
Are you doing any work in the audio callbacks that could be taking a long time?
The callbacks run on realtime threads. if your processing takes too long you can miss data. Would be helpful to track the data over a fixed period of time to see are you capturing it all. Use something like wireShark to sniff the network. Record the number of packets and see did the phone capture the same.
Are you modifying any of the audio?
Do you have a circular buffer that might be causing an issue?
I've had several issues doing this and one was using a third party circular buffer that was described as low latency and efficient ... it wasn't. I answered my own question here and included my circular buffer implementation that greatly improved my audio as the issue was I was skipping data.
Give this a go and let me know:
iOS UI are causing a glitch in my audio stream
Please be aware that some of this code is unique to the audio format ALaw, 0xD5 is a byte of silence in ALaw, if you are using linear PCM or any other that will probably be a noise of some kind.
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?
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.
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!!!
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.
Well I am having two issues that i can't get to work, related to audio and calls.
The first one is to play a sound during a phone call. I don't want to play continuous music or stream anything, it is just a simple and short sound that the user will hear at one time during his call.
I have read some posts claiming that this is possible, and I even have an application that does so, but I can't get it to work. My app identifies the call using CTCallCenter and print the logs but never plays the sound or plays it after the app comes to foreground again. I have the .plist property of required background mode App plays audio.
The second issue, is to play the sound after some elapsed time. NSTimers doesn't work when on background mode, nor NSThread sleep on my background process or NSOperation. So how could I play this sound after say 10 seconds of the call?
Also, this behavior has to work also when the application is already on background mode. With CTCallCenter I am only getting the event when the application is interrupted from use, but I don't see any logs when i send the app to background and then begin/receive a call
.
If anyone could point me to the right direction I'll be really grateful.
I havent done this, but NSLocalNotifcation, schedule a notification to play when you app get the call to move into the background. I would expect this to work. Interested to find out if it does.
I am developing an iphone app which uses background audio (on an infinite loop) to continue playing after the app has entered the background.
My problem is I want to implement a "sleep timer" which stops playback after a specified period of time.
Is this possible? I have spent an hour looking for a method to do this with no avail.
EDIT: My current thought is to use a lower level API, the Audio Queue Services, and manually re-fill the queue with another instance of the loop during the AudioQueueOutputCallback. If the timer has expired I do not fill the loop. I'm assuming this should work since the documentation says audio callbacks are still fired when an app is playing multitasking background audio. Can anyone think of a better way or a reason why this wouldn't work?
While you queue sound data on the background your app remains fully functional and running as if it was in the foreground (well almost), so yes, you should just write a timer that stops the playback at a given time and it will be fired as expected.
Now to the second question: once you stop queueing things up, your app will be "frozen" until the user manually brings it to the foreground... So what you should do is start queueing audio data from the second file before the first one is done playing, and if you DO need to pause or stop, maybe a solution is to play 0 bytes (silence)?
I'm not actually sure this would be allowed in the App Store. An app is not allowed to execute at all in the background, with the exception of VoIP apps and push notifications.
Is it possible to overwrite a part of the recording with another recording.
When a recording is done, user gets back to a particular point of time in recording.
And right from that time, he initiates a recording, and this should overwrite the earlier recording.
Is this possible with AVFoundation Framework available in iPhone?