0

We are using third party Audio/Video SDK in our Xamarin.Forms.iOSproject. Now problem is that by default audio comes in speaker mode, instead of ear-speaker. I found below code and using that audio is toggling in ear-speaker and speaker. but when I am on speaker then microphone is muted and when I am in ear-speaker then both are working. so my question is how to enable microphone in both case?

   bool blIsOnEarSpeaker=false;

    public void SetAudioSettingsForIOS()
    {
        var session=AVFoundation.AVAudioSession.SharedInstance();

       AVFoundation.AVAudioSessionCategory objCategory= AVFoundation.AVAudioSessionCategory.Playback;
        Foundation.NSError error = null;

        if (blIsOnEarSpeaker==false)
        {
            objCategory=AVFoundation.AVAudioSessionCategory.PlayAndRecord;
            blIsOnEarSpeaker=true;

            error = session.SetCategory(objCategory);
        }
        else
        {
            objCategory=AVFoundation.AVAudioSessionCategory.Playback;
            blIsOnEarSpeaker=false;

            error = session.SetCategory(
                                        objCategory
                                       ,AVFoundation.AVAudioSessionCategoryOptions.DefaultToSpeaker
                                       );
        }


        error = session.SetActive(true);
    }
John Smith
  • 139
  • 8

1 Answers1

1

In the else condition:

Change

**objCategory=AVFoundation.AVAudioSessionCategory.Playback;**

to

**objCategory=AVFoundation.AVAudioSessionCategory.PlayAndRecord;**

OR better approach would be, using this line before if/else condition :

bool blIsOnEarSpeaker=false;

public void SetAudioSettingsForIOS()
{
    var session=AVFoundation.AVAudioSession.SharedInstance();

   AVFoundation.AVAudioSessionCategory objCategory= AVFoundation.AVAudioSessionCategory.Playback;
    Foundation.NSError error = null;

    objCategory=AVFoundation.AVAudioSessionCategory.PlayAndRecord;

    if (blIsOnEarSpeaker==false)
    {
        blIsOnEarSpeaker=true;

        error = session.SetCategory(objCategory);
    }
    else
    {
        blIsOnEarSpeaker=false;

        error = session.SetCategory(
                                    objCategory
                                   ,AVFoundation.AVAudioSessionCategoryOptions.DefaultToSpeaker
                                   );
    }


    error = session.SetActive(true);
}
Vipul
  • 21
  • 4
  • This solution solved the problem of whenever I triggered microphone input, the iPhone would disable speakerphone mode, lowering the audio volume so it was impossible to hear. I looked everywhere for this solution! – Bruce Haley Jun 02 '23 at 18:25