Skip to main content
Version: 0.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 Meeting class, the host can publish their audio to other hosts and audience members.

  • You can also pass a customised audio track in the unmuteMic() method by using Custom Audio Track.

muteMic()​

  • By using the muteMic() function of the Meeting class, the host can stop publishing their audio to other hosts and audience members.

Example​

  private var micEnabled = false

btnMic!!.setOnClickListener { _: View? ->
if (micEnabled) {
// Muting Mic
liveStream!!.muteMic()
micEnabled=false
} else {
// Unmuting Mic
liveStream!!.unmuteMic()
micEnabled=true
}
}

Events associated with unmuteMic​

  • Every Participant—including all the hosts and audience members will receive a callback on onStreamEnabled() of the Participant with Stream object.

Events associated with muteMic​

  • Every Participant—including all the hosts and audience members will receive a callback on onStreamDisabled() of the Participant with Stream object.
  meeting!!.localParticipant.addEventListener(object : ParticipantEventListener() {
//Callback for when the participant starts a stream
override fun onStreamEnabled(stream: Stream) {
if(stream.getKind().equals("audio")){
Log.d("VideoSDK","Audio Stream On: onStreamEnabled $stream");
}
}

//Callback for when the participant stops a stream
override fun onStreamDisabled(stream: Stream) {
if(stream.getKind().equals("audio")){
Log.d("VideoSDK","Audio Stream On: 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>.
AndroidManifest.xml
<uses-permission android:name="android.permission.RECORD_AUDIO" />
  • 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