【问题标题】:CADislayLink does not trigger if device is locked如果设备被锁定,CADislayLink 不会触发
【发布时间】:2015-12-06 16:05:37
【问题描述】:

编辑:如果应用程序在后台运行,则使用 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];

无论我们使用的是AVAudioSessionCategoryRecord 还是AVAudioSessionCategoryPlayAndRecord,下面的代码都可以完美地与AVAudioRecorder 配合使用。

原始问题:到目前为止,我正在创建一个记录声音的应用程序,即使它处于后台模式。这很像iTalk

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

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

这是我配置 AVAudioSessionAVAudioRecorder 的代码

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

如果您能提供任何信息,我将不胜感激。谢谢大佬!

【问题讨论】:

    标签: ios triggers sleep cadisplaylink


    【解决方案1】:

    您必须像这样设置AVAudioSession 的类别

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:&sessionError];
    

    以便在设备锁定时录制音频。

    正如文档所说

    在您的应用转换到 背景(例如,屏幕锁定时),添加音频值 到您的信息属性列表文件中的 UIBackgroundModes 键。

    也请查看此链接

    AVAudioRecorder doesn't record while screen is locked

    【讨论】:

    • 恐怕它不会改变任何东西。 AVAudioSessionCategoryRecordAVAudioSessionCategoryPlayAndRecord 有相同的结果,正如我上面所说的
    • 我不明白为什么它不起作用,因为文档说它会在屏幕锁定时起作用。
    • 你是对的。录音机仍然在设备锁定下工作。问题来自我用来监控录像机的 CADisplayLink。我会更新我的问题。非常感谢!
    • 我认为如果你发布一个新问题会更好,然后它会在顶部并且大多数用户都可以看到它。(只是一个建议)
    • 啊,其实我发现了问题并解决了。所以我认为更新我的问题而不是创建一个新问题可能会更好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多