Why is AVCaptureDevice nil?

I am trying to access the microphone using AVCaptureDevice and handle the incoming sound, but the return value from [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio]always is nil.

I found this question where it looks like they had the same problem, but I obviously did not resolve the sandbox, and I added the .plist file with key com.apple.security.device.microphoneand value YES, so I think I should have access to the microphone.

Here is my code

- (void)configureAudioSession
{
    // Configure the audio session
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryRecord error:&error];

    // Optional - default gives 1024 samples at 44.1kHz
    //[session setPreferredIOBufferDuration:samplesPerSlice/session.sampleRate error:NULL];

    // Configure the capture session (strongly-referenced instance variable, otherwise the capture stops after one slice)
    _captureSession = [[AVCaptureSession alloc] init];

    // Configure audio device input
    //The following line always returns nil
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
    AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc] initWithDevice:device error:nil];

    if (input == nil)
    {
        NSLog(@"nil input");
    }

    [_captureSession addInput:input];

    // Configure audio data output
    AVCaptureAudioDataOutput *output = [[AVCaptureAudioDataOutput alloc] init];
    dispatch_queue_t queue = dispatch_queue_create("Callback", DISPATCH_QUEUE_SERIAL);
    [output setSampleBufferDelegate:self queue:queue];
    [_captureSession addOutput:output];

    // Start the capture session.
    [_captureSession startRunning];
}
+4
source share
1 answer

On the simulator is always always zero. If you use the actual device, it will work.

0
source

All Articles