Hello,
I've converted the Java code to Xamarin, and it just works fine on Android 10 and 11.
It is more recommended to post your issue in Xamarin.Forms-issues to make our Product Group aware of it.
Please refer to the following code:
public void switchSpeakerState()
{
AudioManager audioManager = (AudioManager)GetSystemService(Context.AudioService);
if (isSpeakerOn)
{
isSpeakerOn = false;
if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.S)
{
Utils.setCommunicationDevice(this, Android.Media.AudioDeviceType.BuiltinEarpiece);
}
else
{
audioManager.SpeakerphoneOn = false;
}
}
else
{
isSpeakerOn = true;
if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.S)
{
Utils.setCommunicationDevice(this, Android.Media.AudioDeviceType.BuiltinSpeaker);
}
else
{
audioManager.SpeakerphoneOn = true;
}
}
}
Utils.cs:
public static class Utils
{
public static void setCommunicationDevice(Context context, AudioDeviceType targetDeviceType)
{
AudioManager audioManager = (AudioManager)context.GetSystemService(Context.AudioService);
var devices = audioManager.AvailableCommunicationDevices;
foreach (AudioDeviceInfo device in devices)
{
if (device.Type == targetDeviceType)
{
bool result = audioManager.SetCommunicationDevice(device); // the result will be true
Log.Debug("result: ", result.ToString());
}
}
}
}
Best Regards,
Alec Liu.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.