How to switch audio output between bluetooth and speaker? (Both Android and IOS)

Luiz Arruda 20 Reputation points
2024-07-23T16:31:34.9333333+00:00

Is there a way to choose or switch audio output between bluetooth and speaker before playing and audio file ? (Both IOS and Android)

Thx.

Developer technologies .NET .NET MAUI
Developer technologies Visual Studio Other
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2024-07-24T06:17:59.1233333+00:00

    Hello,

    Changing the playback device in Android can only be done in call mode. In iOS, users can only select the playback device through the control center.

    On the Android platform, you can refer to the following code to set the call mode and change the playback device.

    Step 1. Instantiate MainActivity.

    
    public class MainActivity : MauiAppCompatActivity
    
    {
    
        public static MainActivity Instance { get; private set; }
    
        protected override void OnCreate(Bundle? savedInstanceState)
    
        {
    
            base.OnCreate(savedInstanceState);
    
            Instance = this;
    
        }
    
    }
    
    

    Step 2. Create the AudioHelper class in Android folder.

    
    public static class AudioHelper
    
    {
    
        public static void PlayWithSpeaker()
    
        {
    
            AudioManager audioManager = (AudioManager)MainActivity.Instance.GetSystemService(Context.AudioService);
    
            audioManager.Mode = Mode.InCommunication;
    
            audioManager.BluetoothScoOn = false;
    
            audioManager.SpeakerphoneOn = true;
    
        }
    
     
    
        public static void PlayWithBluetooth()
    
        {
    
            AudioManager audioManager = (AudioManager)MainActivity.Instance.GetSystemService(Context.AudioService);
    
            audioManager.Mode = Mode.InCommunication;
    
            audioManager.BluetoothScoOn = true;
    
            audioManager.SpeakerphoneOn = false;
    
            audioManager.StartBluetoothSco();
    
     
    
        }
    
    }
    
    

    Step 3. Invoke the method on maui.

    
    #if ANDROID
    
                AudioHelper.PlayWithSpeaker();
    
    #endif
    
    

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.