Ios 如果设备已锁定,则不会触发CADislayLink

Ios 如果设备已锁定,则不会触发CADislayLink,ios,triggers,sleep,cadisplaylink,Ios,Triggers,Sleep,Cadisplaylink,编辑:如果应用程序在后台运行,使用CADislayLink监控AVAudioRecorder仪表不是一个好主意。如果设备休眠,它将停止触发(在我的例子中是锁定设备)。解决方法是使用NSTimer。这是导致我的问题的代码 - (void)startUpdatingMeter { // Using `CADisplayLink` here is not a good choice. It stops triggering if lock the device self.meterUp

编辑:如果应用程序在后台运行,使用
CADislayLink
监控
AVAudioRecorder
仪表不是一个好主意。如果设备休眠,它将停止触发(在我的例子中是锁定设备)。解决方法是使用
NSTimer
。这是导致我的问题的代码

- (void)startUpdatingMeter {
    // Using `CADisplayLink` here is not a good choice. It stops triggering if lock the device
    self.meterUpdateDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleAudioRecorderMeters)];
    [self.meterUpdateDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
解决方案:改用NSTimer

  // Set timeInterval as frame refresh interval
  self.timerMonitor = [NSTimer timerWithTimeInterval:1.f/60.f target:self selector:@selector(handleAudioRecorderMeters:) userInfo:nil repeats:NO];
  [[NSRunLoop mainRunLoop] addTimer:self.timerMonitor forMode:NSDefaultRunLoopMode];
无论我们使用的是
AVAudioRecorder
还是
AVAudioSessionCategoryRecord
AVAudioSessionCategoryPlayAndRecord
,下面的代码都与
AVAudioRecorder完美配合

原始问题:到目前为止,我正在创建一个录制声音的应用程序,即使是在后台模式下。很像

一切都近乎完美,我的应用程序可以在前台/后台录制(通过注册后台模式),但如果设备被锁定(由用户或自行设备锁定),它会暂停/停止

我试过iTalk,在那种情况下效果很好。我还从iTalk得到一个提示:它在锁屏上有音乐控制,而我的应用程序没有

这是我配置
AVAudioSession
AVAudioRecorder

- (void)configurateAudioSession {
    NSError *error = nil;
    // Return success after set category
    BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDuckOthers error:&error];
    // Return success after set active
    success = [[AVAudioSession sharedInstance] setActive:YES error:&error];
    // Return success after set mode
    success = [[AVAudioSession sharedInstance] setMode:AVAudioSessionModeVideoRecording error:&error];
}

- (void)configAudioRecorder {
    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath_ = [searchPaths objectAtIndex:0];
    NSString *pathToSave = [documentPath_ stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]];

    // Create audio recorder
    NSURL *url = [NSURL fileURLWithPath:pathToSave];
    NSDictionary *settings = @{ AVSampleRateKey: @44100.0,
                                AVFormatIDKey: @(kAudioFormatAppleLossless),
                                AVNumberOfChannelsKey: @1,
                                AVEncoderAudioQualityKey:@(AVAudioQualityMax), };

    NSError *error = nil;
    self.audioRecorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
    if (error) {
        NSLog(@"Error on create audio: %@", error);
    }
    else {
        [self.audioRecorder prepareToRecord];
        self.audioRecorder.meteringEnabled  = YES;
        [self.audioRecorder record];
    }
}

如果您能提供任何信息,我将不胜感激。谢谢你,伙计

您必须像这样设置
音频会话的类别

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:&sessionError];
以便在设备锁定时录制音频

如医生所说

要在应用程序转换到应用程序时继续录制音频,请执行以下操作: 背景(例如,当屏幕锁定时),添加音频值 在信息属性列表文件中输入UIBackgroundModes

也请检查此链接


恐怕这不会改变什么
AVAudioSessionCategoryRecord
AvaudioSessionCategoryPlay和Record
具有相同的结果,正如我上面所说的,我不明白为什么它不工作,因为文档说它会在屏幕锁定时工作。你是对的。录音机仍在设备锁定状态下工作。问题来自CADisplayLink,我使用它来监视记录器。我会更新我的问题。非常感谢!我认为如果你发布一个新问题会更好,然后它会在顶部,大多数用户都能看到。(只是一个建议)啊,实际上我发现了问题并解决了它。因此,我认为最好是更新我的问题,而不是创建一个新的问题。