Reputation: 31
I’m having unexpected issues related to background recording (specifically when the Screen becomes Passcode-locked). In Summary my AVAudioRecorder app seems to record correctly in the foreground, background (when switching apps) and when the screen is off (but before the pass-lock is activated).
If i delay passcode-lock (Settings > Passcode > Require Passcode > 4 hours .etc) then the recording works without a problem in foreground/background/app switching/screen off .etc, but when the pass-lock kicks in the recording becomes corrupt.
I'm basically using AVAudioRecorder code to record the audio, and have followed the guidelines by setting 'UIBackgroundModes/audio' setting in app.plist to ensure background recording, which seems to work in the background (i.e. Red Recording Bar appears) but fails when pass-code becomes active i.e. user pushes the power button and is required to enter a passcode to access the device.
My code is set to handle audio-interrupts via ‘AVAudioSessionInterruptionNotification’ and AVAudioSession is setup ‘withOptions:AVAudioSessionCategoryOptionDuckOthers’ to minimise outside events from effecting background recording.
[ Testing Procedure ]
Passcode-lock (iPhone 5/iOS8.1 > Settings > Passcode > Require Passcode > After 5 minutes)
[Success-CASE-1] ‘Start Recording’ for 7mins Leave Screen On and App active/visible (i.e. not running in background after recording is started) >> Recording works and after pressing ‘Stop Recording’ audio is saved and can be played back.
[Success-CASE-2] ‘Start Recording’ for 7mins Leave Screen On and Switch apps (i.e. run in background with screen mostly On and Red-Recording bar displayed after recording is started) >> Recording works and after pressing ‘Stop Recording’ audio is saved and can be played back.
[Failure-CASE] ‘Start Recording’ for 7mins Switch Screen Off (i.e. app is running in background with devices screen off) >> After 7mins with Screen-Off, device requires passcode when screen is switched-On. Recording unexpectedly stops, becomes corrupt cannot be played back.
Passcode-lock (iPhone 5/iOS8.1> Settings > Passcode > Require Passcode > After 15 minutes)
Only difference between Failure-CASE and Repeat-Failure-CASE is the screen-lock passcode was not required.
[ Notes ]
..
Hope the info helps, took a lot of trial and error to get to this point.
has anyone else experienced this issue? as its quite strange.
thanks
Upvotes: 3
Views: 504
Reputation: 11
Your audio file(in sandbox) maybe is protected by iOS when passcode-lock; If the screen is locked without a passcode set, everything is ok;
Try the below:
NSDictionary *oldAttr = [[NSFileManager defaultManager]attributesOfItemAtPath:localFilePath error:nil];
NSMutableDictionary *newAttr = nil;
if (oldAttr){
newAttr = [[NSMutableDictionary alloc]initWithDictionary:oldAttr];
}else{
newAttr = [[NSMutableDictionary alloc]init];
}
[newAttr setObject:NSFileProtectionNone forKey:NSFileProtectionKey];
[[NSFileManager defaultManager] setAttributes:newAttr
ofItemAtPath:localFilePath
error:nil];
Or,set your Data-protection in Certificate to close。
Upvotes: 1