Skip to main content
Version: 3.x.x

Mute / Unmute Mic - Android

Muting your microphone stops other participants from hearing you. Unmuting it lets them hear you again.

unmuteMic()​

  • Call room.unmuteMic() to start sending the local participant's microphone audio to the room. Call it while the mic is muted.

  • It throws VideoSDKException when the room's mode is RECV_ONLY or SIGNALLING_ONLY, because those modes send no audio.

  • You can pass a custom audio track, created with VideoSDK.createMicrophoneAudioTrack(), to unmuteMic(). Learn more in Custom Audio Track.

  • The participant's audio stream arrives on the onStreamEnabled event of ParticipantEventListener.

muteMic()​

  • Call room.muteMic() to stop sending the local participant's audio. Call it while the mic is on.

  • The microphone is released, so the system microphone indicator goes off.

note
  • unmuteMic() and muteMic() are suspend functions. Call them from a coroutine, such as lifecycleScope.launch (from androidx.lifecycle:lifecycle-runtime-ktx).
  • They throw VideoSDKException with 3054 (ERROR_ALREADY_IN_REQUESTED_STATE) when the mic is already in the requested state, and with 3055 (ERROR_OPERATION_IN_PROGRESS) while another mic toggle is still running.
  • Called before the room is joined, or while it is reconnecting, they do nothing and report 3035 (ERROR_ACTION_PERFORMED_BEFORE_ROOM_JOINED) or 3065 (ERROR_ROOM_RECONNECTING) on onError instead of throwing.
  • room.isMicEnabled tells you whether the mic is on.

Example​

btnMic!!.setOnClickListener {
lifecycleScope.launch {
try {
if (room!!.isMicEnabled) {
// Muting Mic
room!!.muteMic()
} else {
// Unmuting Mic
room!!.unmuteMic()
}
} catch (e: VideoSDKException) {
Log.e("VideoSDK", "Mic toggle failed: ${e.code} ${e.name}")
}
}
}

Events associated with unmuteMic​

  • On every device in the room, onStreamEnabled() is called on the listener of that Participant, with the new RTCStream. Its kind is MediaType.AUDIO.

Events associated with muteMic​

  • On every device in the room, onStreamDisabled() is called on the listener of that Participant, with the RTCStream that stopped. Its kind is MediaType.AUDIO.
room!!.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 room, declare RECORD_AUDIO in app/src/main/AndroidManifest.xml, inside <manifest> and outside <application>. Also add MODIFY_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 MainActivity : AppCompatActivity() {
private val PERMISSION_REQUEST_CODE: Int = 1

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

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