Mute / Unmute Mic - Android
This feature allows hosts to communicate with each other during the livestream by enabling or disabling their microphones. While only hosts (participants in SEND_AND_RECV mode) can speak, audience members (in RECV_ONLY mode) can listen to the conversation in real time.
unmuteMic()
-
By using the
unmuteMic()function of theRoomclass, the host can publish their audio to other hosts and audience members. -
You can also pass a customised audio track, created with
VideoSDK.createMicrophoneAudioTrack(), in theunmuteMic()method by using Custom Audio Track.
muteMic()
- By using the
muteMic()function of theRoomclass, the host can stop publishing their audio to other hosts and audience members.
note
unmuteMic()andmuteMic()aresuspendfunctions. Call them from a coroutine, such aslifecycleScope.launch(fromandroidx.lifecycle:lifecycle-runtime-ktx).- They throw
VideoSDKExceptionwith3054(ERROR_ALREADY_IN_REQUESTED_STATE) when the mic is already in the requested state, and with3055(ERROR_OPERATION_IN_PROGRESS) while another mic toggle is still running. - Only
SEND_AND_RECVmode can publish audio. Called by an audience member (RECV_ONLYmode), they throwVideoSDKExceptionwith3066(ERROR_ACTION_NOT_SUPPORTED_IN_MODE). - Called before the live stream is joined, or while it is reconnecting, they do nothing and report
3035(ERROR_ACTION_PERFORMED_BEFORE_ROOM_JOINED) or3065(ERROR_ROOM_RECONNECTING) ononErrorinstead of throwing. liveStream.isMicEnabledtells you whether the mic is on.
Example
btnMic!!.setOnClickListener {
lifecycleScope.launch {
try {
if (liveStream!!.isMicEnabled) {
// Muting Mic
liveStream!!.muteMic()
} else {
// Unmuting Mic
liveStream!!.unmuteMic()
}
} catch (e: VideoSDKException) {
Log.e("VideoSDK", "Mic toggle failed: ${e.code} ${e.name}")
}
}
}
Events associated with unmuteMic
- Every participant, hosts and audience alike, receives
onStreamEnabled()on thatParticipant's listener, with theRTCStream.
Events associated with muteMic
- Every participant, hosts and audience alike, receives
onStreamDisabled()on thatParticipant's listener, with theRTCStream.
liveStream!!.localParticipant.addEventListener(object : ParticipantEventListener() {
//Callback for when the participant starts a stream
override fun onStreamEnabled(stream: RTCStream) {
if (stream.kind == MediaType.AUDIO) {
Log.d("VideoSDK", "Audio Stream On: onStreamEnabled $stream")
}
}
//Callback for when the participant stops a stream
override fun onStreamDisabled(stream: RTCStream) {
if (stream.kind == MediaType.AUDIO) {
Log.d("VideoSDK", "Audio Stream Off: onStreamDisabled $stream")
}
}
})
Audio Permissions
- To use the microphone in a live stream, you need to add permission in
app/src/main/AndroidManifest.xmlafter</application>. Also addMODIFY_AUDIO_SETTINGS: the SDK needs it to route audio, and it does not add it to your manifest.
AndroidManifest.xml
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
- You need to set up a permission request that provides this access.
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
class LiveStreamActivity : AppCompatActivity() {
private val PERMISSION_REQUEST_CODE: Int = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_live_stream)
if (checkPermission()) {
// . write your main code to execute, It will execute if the permission is already given.
} else {
requestPermission()
}
}
private fun checkPermission(): Boolean {
return ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
}
private fun requestPermission() {
ActivityCompat.requestPermissions(
this, arrayOf(Manifest.permission.RECORD_AUDIO),
PERMISSION_REQUEST_CODE
)
}
override
fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
PERMISSION_REQUEST_CODE -> if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(applicationContext, "Permission Granted", Toast.LENGTH_SHORT).show()
// main logic
} else {
Toast.makeText(applicationContext, "Permission Denied", Toast.LENGTH_SHORT).show()
}
}
}
}
API Reference
The API references for all the methods and events utilised in this guide are provided below.
Got a Question? Ask us on discord

