Skip to main content
Version: 3.x.x

Mic Controls - Android

A participant can start or stop sending their audio to other participants in a meeting at any time.

This guide shows how to enable and disable the mic in a meeting.

  1. Enable Mic - By using unmuteMic() function, a participant can publish audio to other participants.
  2. Disable Mic - By using muteMic() function, a participant can stop publishing audio to other participants.

1. Enable, Disable Mic​

Both methods are suspend functions. Call them from a coroutine, such as lifecycleScope.launch (from androidx.lifecycle:lifecycle-runtime-ktx), and catch the VideoSDKException they throw when a call is refused or fails.

btnMic!!.setOnClickListener { _: View? ->
lifecycleScope.launch {
try {
// Toggle participant mic in meeting
if (meeting!!.isMicEnabled) {
meeting!!.muteMic()
} else {
meeting!!.unmuteMic()
}
} catch (e: VideoSDKException) {
Log.e("VideoSDK", "Mic toggle failed: ${e.code} ${e.name}")
}
}
}

Events​

Event associated with unmuteMic():

Event associated with muteMic():

meeting!!.localParticipant.addEventListener(object : ParticipantEventListener() {
override fun onStreamEnabled(stream: Stream) {
if (stream.kind.equals("audio", ignoreCase = true)) {
//participant turned on audio
}
}

override fun onStreamDisabled(stream: Stream) {
if (stream.kind.equals("audio", ignoreCase = true)) {
//participant turned off audio
}
}
})

2. Change Audio Device​

Get all audio Devices​

  • By using meeting.mics property, a participant can get all the connected mics.
private fun getMics(): Set<AppRTCAudioManager.AudioDevice> {
val mics = meeting!!.mics
return mics // returns all mics
}

Select audio Device​

  • Local participant can change the audio device using changeMic(device: AppRTCAudioManager.AudioDevice?) method of meeting class. It is a suspend function. It throws VideoSDKException if the device is not connected.

  • While the mic is on, changeMic() mutes and unmutes it on the new device, so every participant receives onStreamDisabled() and then onStreamEnabled().

  • Parameters that can be passed to changeMic are

    Audio DevicesUsage
    AppRTCAudioManager.AudioDevice.BLUETOOTHFor Bluetooth Device.
    AppRTCAudioManager.AudioDevice.WIRED_HEADSETFor Wired Headset Device.
    AppRTCAudioManager.AudioDevice.SPEAKER_PHONEFor Inbuilt - Speaker Device
    AppRTCAudioManager.AudioDevice.EARPIECEFor Earpiece Device
  • When a Local participant changes the Mic, AppRTCAudioManager.AudioManagerEvents is triggered which can be set to Meeting class by using meeting.setAudioDeviceChangeListener(). It is also called once as soon as you set it, with the current device.


btnChangeMic!!.setOnClickListener { _: View? ->
lifecycleScope.launch {
try {
// Change Mic in Meeting
meeting!!.changeMic(AppRTCAudioManager.AudioDevice.BLUETOOTH)
} catch (e: VideoSDKException) {
Log.e("VideoSDK", "Mic change failed: ${e.code} ${e.name}")
}
}
}

private fun setAudioDeviceListeners() {
meeting!!.setAudioDeviceChangeListener(object : AudioManagerEvents {
override fun onAudioDeviceChanged(
selectedAudioDevice: AppRTCAudioManager.AudioDevice,
availableAudioDevices: Set<AppRTCAudioManager.AudioDevice>
) {
when (selectedAudioDevice) {
AppRTCAudioManager.AudioDevice.BLUETOOTH ->
Toast.makeText(this@MainActivity, "Selected AudioDevice: BLUETOOTH", Toast.LENGTH_SHORT).show()

AppRTCAudioManager.AudioDevice.WIRED_HEADSET ->
Toast.makeText(this@MainActivity, "Selected AudioDevice: WIRED_HEADSET", Toast.LENGTH_SHORT).show()

AppRTCAudioManager.AudioDevice.SPEAKER_PHONE ->
Toast.makeText(this@MainActivity, "Selected AudioDevice: SPEAKER_PHONE", Toast.LENGTH_SHORT).show()

AppRTCAudioManager.AudioDevice.EARPIECE ->
Toast.makeText(this@MainActivity, "Selected AudioDevice: EARPIECE", Toast.LENGTH_SHORT).show()

AppRTCAudioManager.AudioDevice.NONE -> {
// no audio device is selected yet
}
}
}
})
}

override fun onCreate(savedInstanceState: Bundle?) {
// ...
setAudioDeviceListeners()
}
  • To use Bluetooth device, you must declare BLUETOOTH permission in AndroidManifest.xml file. On Android 12 and later, also request BLUETOOTH_CONNECT at runtime, for example with VideoSDK.checkPermissions() and Permission.bluetooth.
<manifest ... >

<!-- Needed to communicate with already-paired Bluetooth devices. (Legacy up to Android 11) -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<!-- ... -->
</manifest>

Got a Question? Ask us on discord