问题
We are using third party Audio/Video SDK in our Xamarin.Forms.Android project. Now problem is that by default audio comes in speaker mode, instead of ear-speaker. I was tried to search solution in internet and found below code to set audio in ear-speaker and speaker. After implement below code audio off in speaker but not able to listen via ear-speaker, if I set SpeakerphoneOn=true then I can listen via Speakerphone but if I set SpeakerphoneOn=false then I can't list via ear-speaker.
Additional point, Mode value is always comes "InCommunication" even after change to InCall or any other mode.
I have set below permission too in my Xamarin project. Permission:
var audioManager = (Android.Media.AudioManager)Android.App.Application.Context.GetSystemService(Android.Content.Context.AudioService);
audioManager.SpeakerphoneOn = !audioManager.SpeakerphoneOn;
回答1:
Check when mode is ear-speaker , the volume value whether is 0 .The volume of their two modes is controlled separately.
var audioManager = (Android.Media.AudioManager)Android.App.Application.Context.GetSystemService(Android.Content.Context.AudioService);
audioManager.Mode = Mode.InCommunication;
audioManager.SpeakerphoneOn = false;
audioManager.SetStreamVolume(Stream.Music, audioManager.GetStreamMaxVolume(Stream.Music), VolumeNotificationFlags.ShowUi);
来源:https://stackoverflow.com/questions/55649921/toggle-audio-in-speaker-to-ear-speaker-and-ear-speaker-to-speaker