Skip to main content
Version: 3.x.x

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 the Room class, 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 the unmuteMic() method by using Custom Audio Track.

muteMic()​

  • By using the muteMic() function of the Room class, the host can stop publishing their audio to other hosts and audience members.
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.
  • Only SEND_AND_RECV mode can publish audio. Called by an audience member (RECV_ONLY mode), they throw VideoSDKException with 3066 (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) or 3065 (ERROR_ROOM_RECONNECTING) on onError instead of throwing.
  • liveStream.isMicEnabled tells 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​

Events associated with muteMic​

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.xml after </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 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