3
votes

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)

    1. [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.

    2. [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.

    3. [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)

    1. [Repeat-Failure-CASE with Adjusted Passlock] ‘Start Recording’ for 7mins Switch Screen Off (i.e. app is running in background with devices screen off) >> After 7mins with Screen-Off, device is switched on (no pass lock is needed this time). Recording is okay, works perfectly and can be played back.

Only difference between Failure-CASE and Repeat-Failure-CASE is the screen-lock passcode was not required.

[ Notes ]

  1. Tested using XCode 6.1, on iPhone 4/5/6, iPad Mini 2 (iOS8.1) with same issue.
  2. Code is standard AVAudioRecorder code, with AVAudioSession setup similar to: http://www.jacopretorius.net/2013/04/record-and-playback-audio-in-ios.html

..

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

1

1 Answers

1
votes

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。