Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

objective c - AVAudioRecorder doesn't record while screen is locked

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...