Audio Output Routing

Audio Output Routing is helpful when users want to switch the audio output to a connected device other than the default one.

For example, you can implement an in-call speaker button that toggles audio output between the speaker, earpiece or any other connected earphones - wired or wireless, etc.

Let's see how we can use hmsSDK to implement this:

Fetch Available Audio Devices

Invoke the getAudioDevicesList method to fetch the available audio device list. The method returns a list of HMSAudioDevice enum.

List<HMSAudioDevice> availableDevices = await hmsSDK.getAudioDevicesList(); enum HMSAudioDevice { SPEAKER_PHONE, EARPIECE, WIRED_HEADSET, // Android only BLUETOOTH, // Android only AUTOMATIC, // Android only UNKNOWN }

The getAudioDevicesList API returns different values based on the Android & iOS platforms. It can return the following HMSAudioDevice enum values :

- For Android

  • SPEAKER_PHONE - route audio to the device speaker
  • EARPIECE - route audio to earpiece
  • WIRED_HEADSET - route audio to a connected wired device
  • BLUETOOTH - route audio to connected Bluetooth device
  • AUTOMATIC - automatic routing

- For iOS

  • SPEAKER_PHONE - route audio to device speaker
  • EARPIECE - route audio earpiece

Switch Audio Focus to Another Device

Invoke the switchAudioOutput method with the appropriate HMSAudioDevice value fetched from getAudioDevicesList to switch the audio to that device.

The audioDevice is a required HMSAudioDevice enum type parameter which we discussed above.

///[audioDevice]: is an enum of type `HMSAudioDevice` hmsSDK.switchAudioOutput(audioDevice: audioDevice);

Receive the device change updates (Android Only)

Implementing the onAudioDeviceChanged listener allows you to get event updates if a new audio device gets connected such as Bluetooth, Wired Headset etc. This method needs to be overridden in the class where HMSUpdateListener is implemented.

Note that this event is available only on the Android platform.

class Meeting implements HMSUpdateListener, HMSActionResultListener{ ... void onAudioDeviceChanged( {HMSAudioDevice? currentAudioDevice, List<HMSAudioDevice>? availableAudioDevice}) { // currentAudioDevice : audio device to which audio is curently being routed to // availableAudioDevice : all other available audio devices } }

Get Current Focussed Device (Android Only)

The getCurrentAudioDevice method allows you to get the device through which audio output is currently being routed to.

HMSAudioDevice currentAudioDevice = await hmsSDK.getCurrentAudioDevice();

Note: onAudioDeviceChanged listener and getCurrentAudioDevice methods are available only on the Android platform.

Switch Audio Output Device UI (iOS Only)

The switchAudioOutputUsingiOSUI allows you to display the native iOS UI for switching the audio output device. Something similar to this:

Switch Audio Output

Let's look at the implementation:

///[hmsSDK] is the HMSSDK instance hmsSDK.switchAudioOutputUsingiOSUI();

Have a suggestion? Recommend changes ->

Was this helpful?