【问题标题】:AVAudioRecorder doesn't record while screen is locked屏幕锁定时 AVAudioRecorder 不录制
【发布时间】:2023-03-29 04:21:01
【问题描述】:

我已经尝试克服这个问题一段时间了。我正在尝试录制声音,但屏幕锁定时 AVAudioRecorder 不录制。一旦屏幕解锁,它会继续录制,但屏幕锁定时录制的音频将永远丢失。我没有发现我正在做的事情有什么问题:

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

}

有什么想法吗?

【问题讨论】:

  • 在另一个线程中发现了这个,稍后会尝试:“你大概需要在 Info 中设置 UIBackgroundModesaudio .plist"
  • 好的 - 刚刚尝试了我之前的评论,它成功了!太糟糕了,我在 AVAudioRecorder 或 AVAudioSession 开发人员文档中找不到它。这是我找到它的链接:[stackoverflow.com/questions/3848172/…
  • 我希望我能将自己标记为这个问题的回答者 :)
  • 您能否发布您尝试过的解决方案并将其标记为答案,以便对其他人有所帮助?
  • 哦,我刚刚意识到我确实可以回答我自己的问题——我是新来的。好的,我会发帖的。

标签: objective-c ios audio-recording avaudiorecorder avaudiosession


【解决方案1】:

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

这是not-so-easy to find documentation。不幸的是,苹果没有在他们声称某些类别在后台工作的音频会话类别的文档中指定这一点。无论如何,希望这个答案能提供给其他有类似问题的人......

【讨论】:

    【解决方案2】:

    您可能需要考虑将类别设置为 AVAudioSessionCategoryRecordAudioSession

    【讨论】:

    • 有谁知道如何解决这个问题。我确实打开了音频的背景模式并设置了正确的类别。但它仍然不起作用。提前致谢
    【解决方案3】:

    在录制完成之前禁用屏幕锁定怎么样?

    [UIApplication sharedApplication].idleTimerDisabled = YES;
    
    // Do recording here
    
    [UIApplication sharedApplication].idleTimerDisabled = NO;
    

    完成后别忘了重新启用屏幕锁定!

    【讨论】:

    • 不幸的是,我想做的恰恰相反:我想长时间录制,所以我想在屏幕锁定时进行以节省电池...
    猜你喜欢
    • 2015-06-07
    • 2013-04-15
    • 2016-05-28
    • 2015-11-29
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多