adrianstevens / Xamarin-Plugins

Cross-platform Plugins for Xamarin, Xamarin.Forms and Windows
https://www.nuget.org/packages/Xam.Plugin.SimpleAudioPlayer/
MIT License
132 stars 53 forks source link

Too low volume on iOS #83

Closed piotrbalut closed 3 years ago

piotrbalut commented 3 years ago

I have a problem with volume on iOS - it's too low. On Android everything work great. I tried something like this in AppDelegate, but the error still exists.

        void SetAudioSettings()
        {
            NSError error = AVFoundation.AVAudioSession.SharedInstance().SetCategory(AVFoundation.AVAudioSessionCategory.PlayAndRecord, AVFoundation.AVAudioSessionCategoryOptions.DefaultToSpeaker);

            if (error == null)
            {
                if (AVFoundation.AVAudioSession.SharedInstance().SetMode(AVFoundation.AVAudioSession.ModeVideoChat, out error))
                {
                    if (AVFoundation.AVAudioSession.SharedInstance().OverrideOutputAudioPort(AVFoundation.AVAudioSessionPortOverride.Speaker, out error))
                    { 
                        error = AVFoundation.AVAudioSession.SharedInstance().SetActive(true);

                        if (error != null)
                        {
                            Logger.Log(new Exception(error?.LocalizedDescription ?? "Cannot set active"));
                        }
                    }
                    else
                    {
                        Logger.Log(new Exception(error?.LocalizedDescription ?? "Cannot override output audio port"));
                    }
                }
                else
                {
                    Logger.Log(new Exception("Cannot set mode"));
                }
            }
            else
            {
                Logger.Log(new Exception(error?.LocalizedDescription ?? "Cannot set category"));
            }
        }

Any idea?

piotrbalut commented 3 years ago

I resolved this using:

        void SetAudioSettings()
        {
            NSError error = AVFoundation.AVAudioSession.SharedInstance().SetCategory(AVFoundation.AVAudioSessionCategory.PlayAndRecord, AVFoundation.AVAudioSessionCategoryOptions.DefaultToSpeaker);

            if (error == null)
            {
                if (AVFoundation.AVAudioSession.SharedInstance().SetMode(AVFoundation.AVAudioSession.ModeSpokenAudio, out error))
                {
                    if (AVFoundation.AVAudioSession.SharedInstance().OverrideOutputAudioPort(AVFoundation.AVAudioSessionPortOverride.Speaker, out error))
                    { 
                        error = AVFoundation.AVAudioSession.SharedInstance().SetActive(true);

                        if (error != null)
                        {
                            Logger.Log(new Exception(error?.LocalizedDescription ?? "Cannot set active"));
                        }
                    }
                    else
                    {
                        Logger.Log(new Exception(error?.LocalizedDescription ?? "Cannot override output audio port"));
                    }
                }
                else
                {
                    Logger.Log(new Exception("Cannot set mode"));
                }
            }
            else
            {
                Logger.Log(new Exception(error?.LocalizedDescription ?? "Cannot set category"));
            }
        }