-->

RPScreenRecorder startCaptureWithHandler: not retu

2019-08-06 19:33发布

问题:

I am trying to use RPScreenRecorder startCaptureWithHandler:completionHandler: api on my iPad with iOS 11.4 to grab audio and video directly. It works well with the app screen, app audio and the camera, but when I turn on the microphone using microphoneEnabled = YES, I never get any audio sample for the mic in the callback.

I added the microphone privacy usage key in the info.plist but that did not help.

I am not sure what I can do next to try to resolve this issue.

Thanks.

回答1:

This code gets me audio buffers containing actual microphone audio (even without a microphone usage string or audio session configuration):

RPScreenRecorder *recorder = [RPScreenRecorder sharedRecorder];

recorder.microphoneEnabled = YES;

[recorder startCaptureWithHandler:^(CMSampleBufferRef sampleBuffer, RPSampleBufferType bufferType, NSError* error) {
    NSLog(@"Capture %@, %li, %@", sampleBuffer, (long)bufferType, error);
    if (RPSampleBufferTypeAudioMic == bufferType) {
        // Do something with mic audio
    }
} completionHandler:^(NSError* error) {
    NSLog(@"startCapture: %@", error);
}];

There's a permission dialog that asks whether you Allow Screen & Microphone, Allow Screen Only, or don't allow. Is it possible you tapped screen-only? N.B. I don't know how to reset this dialog.

The other possibility is that you're setting microphoneEnabled after you start capturing? That might not work. Nah, that works for me too.

Previous guess

You might need to activate a recording AVAudioSession:

try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryRecord)  // or AVAudioSessionCategoryPlayAndRecord!
try! AVAudioSession.sharedInstance().setActive(true)