IOS Audio Recording, How to Check if Mic / Playbac

2019-04-02 19:36发布

问题:

If anything is playing, recording, how to we check to see if the MIC is available (idle) for recording? Currently using

AVCaptureDevice *audioCaptureDevice      = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
AVCaptureSession *captureSession         = [[AVCaptureSession alloc] init];
VCaptureDeviceInput *audioInput          = [AVCaptureDeviceInput deviceInputWithDevice : audioCaptureDevice error:&error];
AVCaptureAudioDataOutput    *audioOutput = [[AVCaptureAudioDataOutput alloc] init];
[captureSession addInput  : audioInput];
[captureSession addOutput : audioOutput];
[captureSession startRunning];

Need to check before grabbing the MIC / Playback from something that is already has it.

回答1:

The mic device can not be busy/access to it can not be locked, even if you call [AVCaptureDevice lockForConfiguration] on a mic device it will not lock it and it is still accessible to the foreground application.

To see if other audio is playing you can check kAudioSessionProperty_OtherAudioIsPlaying e.g.:

UInt32 propertySize, audioIsAlreadyPlaying=0;
propertySize = sizeof(UInt32);
AudioSessionGetProperty(kAudioSessionProperty_OtherAudioIsPlaying, &propertySize, &audioIsAlreadyPlaying);

Additionally on Audio Session Programming Guide it is stated: "There is no programmatic way to ensure that an audio session is never interrupted. The reason is that iOS always gives priority to the phone. iOS also gives high priority to certain alarms and alerts"