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.
- Enable Mic - By using
unmuteMic()function, a participant can publish audio to other participants. - 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():
- Every Participant will receive a callback on
onStreamEnabled()of theParticipantwithStreamobject.
Event associated with muteMic():
- Every Participant will receive a callback on
onStreamDisabled()of theParticipantwithStreamobject.
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.micsproperty, 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 asuspendfunction. It throwsVideoSDKExceptionif the device is not connected. -
While the mic is on,
changeMic()mutes and unmutes it on the new device, so every participant receivesonStreamDisabled()and thenonStreamEnabled(). -
Parameters that can be passed to
changeMicareAudio Devices Usage AppRTCAudioManager.AudioDevice.BLUETOOTH For Bluetooth Device. AppRTCAudioManager.AudioDevice.WIRED_HEADSET For Wired Headset Device. AppRTCAudioManager.AudioDevice.SPEAKER_PHONE For Inbuilt - Speaker Device AppRTCAudioManager.AudioDevice.EARPIECE For Earpiece Device -
When a Local participant changes the Mic,
AppRTCAudioManager.AudioManagerEventsis triggered which can be set toMeetingclass by usingmeeting.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
BLUETOOTHpermission inAndroidManifest.xmlfile. On Android 12 and later, also requestBLUETOOTH_CONNECTat runtime, for example withVideoSDK.checkPermissions()andPermission.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

