3

I've tried to overcome this for a while. I'm trying to record sound, but the AVAudioRecorder doesn't record while screen is locked. It does continue to record once screen is unlocked, but the audio recorded when screen was locked is lost forever. I can't find anything wrong with what I'm doing:

-(void) startRecording
{
    // Begin the recording session.
    _session = [AVAudioSession sharedInstance];
    NSError *setCategoryError = nil;

    NSError *startRecordError;
    [_session setActive:YES error:&startRecordError];
    [self GKLog:[NSString stringWithFormat:@"recorder session error? :%@", startRecordError]];

    [_session  setCategory: AVAudioSessionCategoryRecord  error: &setCategoryError];

    if (setCategoryError) { NSLog(@"some error");}

    //set me as delegate    
    _session.delegate=(id <AVAudioSessionDelegate>) self;

    NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
    [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
    [recordSetting setValue :[NSNumber numberWithInt:8]                               forKey:AVEncoderBitRateKey];
    [recordSetting setValue:[NSNumber numberWithFloat:8000.0] forKey:AVSampleRateKey]; 
    [recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];

    if (!self.currentPath)
    {
        NSLog(@"can't record, no path set!");
        return;
    }

    NSError *error;
    NSURL *url=[NSURL fileURLWithPath:self.currentPath];

    //Setup the recorder to use this file and record to it.
    _recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&error];
    [self GKLog:[NSString stringWithFormat:@" recorder:%@",_recorder]];

    _recorder.delegate=(id <AVAudioRecorderDelegate>) self;
    [_recorder prepareToRecord];

    //Start the actual Recording
    [_recorder record];

}

Any ideas, please?

user1258240
  • 855
  • 7
  • 15
  • 1
    found this in another thread, will try it later: "You presumably need to set UIBackgroundModesaudio in Info.plist" – user1258240 Mar 29 '12 at 22:02
  • Ok - just tried my previous comment and it worked! Too bad I couldn't find it in the AVAudioRecorder or AVAudioSession developer documentation. Here's a link to where I did find it: [http://stackoverflow.com/questions/3848172/ios-multitasking-for-an-audio-recording-application] – user1258240 Mar 29 '12 at 22:30
  • I wish I could mark myself as the answerer of this question :) – user1258240 Mar 29 '12 at 22:32
  • Can you post the solution that you tried and mark it as the answer so that it would help others? – grassyburrito Mar 30 '12 at 10:56
  • Oh, I just realized that I can indeed answer my own question - I'm new here.. Ok, will post. – user1258240 Mar 30 '12 at 11:57

3 Answers3

7

Ok, so the answer to my own question, which took me a long time to find out, is the following: The code I posted is good, but to actually work it needs to work in the background after screen was locked. For this one needs to add a UIBackgroundModes array in the app's plist file, and add 'audio' as one of its objects. This tells the system to let the app work with audio in the background.

Here's the not-so-easy to find documentation. Unfortunately apple doesn't specify that in their documentation of the audio session categories where they claim certain categories work in the background. Anyway, hopefully this answer will be available for others who have a similar problem...

user1258240
  • 855
  • 7
  • 15
0

How about disabling the screen lock until you are done recording?

[UIApplication sharedApplication].idleTimerDisabled = YES;

// Do recording here

[UIApplication sharedApplication].idleTimerDisabled = NO;

Just don't forget to re-enable the screen lock when you're done!

Opsimath
  • 1,997
  • 2
  • 18
  • 12
  • 1
    Unfortunately what I'm trying to do is exactly the opposite: I want to record for a long period so I want to do it while screen is locked to conserve battery... – user1258240 Mar 29 '12 at 08:40
0

You may want to consider setting the category as AVAudioSessionCategoryRecord to the AudioSession

grassyburrito
  • 1,213
  • 22
  • 32
  • Has anyone figured out how to solve this. I did turn on background mode for audio and set proper category. But it still doesn't work. Thanks in advance – Thuyen Trinh Feb 26 '16 at 11:07