Question

In my iOS application, I want to show an Alert, if Mic is not available in the device.

How can I check this ?

I have tried this-

 -(void)checkMic
 {
  UInt32 sessionCategory = kAudioSessionCategory_RecordAudio;
   OSStatus status = AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (sessionCategory), &sessionCategory);  //..always seems to return OK..
   status = AudioSessionSetActive (true);
   if(status)
   {
      //mic is not available
    }
  }

But this code is showing an error with message - deprecated.

Was it helpful?

Solution

AudioSessionSetProperty and AudioSessionGetProperty are deprecated as of iOS 7. Use AVAudioSession instead.

You can use its availableInputs property to get a list of what's available and iterate through them to find one that looks like a microphone, like this:

NSArray *availableInputs = [[AVAudioSession sharedInstance] availableInputs];
BOOL micPresent = false;
for (AVAudioSessionPortDescription *port in availableInputs)
{
    if ([port.portType isEqualToString:AVAudioSessionPortBuiltInMic] ||
        [port.portType isEqualToString:AVAudioSessionPortHeadsetMic])
    {
        micPresent = true;
    }
}

if (micPresent)
{
    // Do something cool
}
else
{
    // No mic present - show alert
}

Alternatively, if you just want any input at all, and don't care if it's a mic or a line-in, you can just do:

if ([[AVAudioSession sharedInstance] inputAvailable];
{
    // Do something cool
}
else
{
    // No input present - show alert
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top