本文介绍了屏幕被锁定时,AVAudioRecorder不会录制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图克服这个问题一段时间。我正在尝试录制声音,但AVAudioRecorder在屏幕锁定时不会录制。一旦屏幕解锁,它会继续记录,但屏幕锁定时录制的音频将永久丢失。我找不到任何有问题的事情:

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];

}

有任何想法吗?

推荐答案

好的,所以我花了很长时间才发现问题的答案如下:我发布的代码很好,但是要实际工作,它需要在屏幕锁定后在后台工作。为此,需要在应用程序的plist文件中添加一个UIBackgroundModes数组,并添加audio作为其对象之一。这告诉系统让应用程序在后台运行音频。

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

这篇关于屏幕被锁定时,AVAudioRecorder不会录制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 02:10
查看更多