Mute / Unmute Mic - Android
Muting and Unmuting your microphone refers to turning your microphone off and on, respectively.
When you mute your microphone, you prevent any sound from your microphone from being transmitted to other meeting participants, while unmuting it allows others to hear you.
unmuteMic()
-
By using
unmuteMic()
function ofMeeting
class, local participant can publish audio to other participants.- You can call this method when the local participant is not broadcasting any audio to others.
-
You can pass customised audio track in
unmuteMic()
by using Custom Audio Track. -
Audio stream of the participant can be accessed from the
onStreamEnabled
event ofParticipantEventListener
.
muteMic()
-
By using
muteMic()
function ofMeeting
class, local participant can stop publish audio to other participants. -
You can call this method when the local participant is broadcasting any audio to others.
Example
- Kotlin
- Java
private var micEnabled = false
btnMic!!.setOnClickListener { _: View? ->
if (micEnabled) {
// Muting Mic
meeting!!.muteMic()
micEnabled=false
} else {
// Unmuting Mic
meeting!!.unmuteMic()
micEnabled=true
}
}
private boolean micEnabled = false;
btnMic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (micEnabled) {
// Muting Mic
meeting.muteMic();
micEnabled = false;
} else {
// Unmuting Mic
meeting.unmuteMic();
micEnabled = true;
}
}
});
Events associated with unmuteMic
- Every Participant will receive a callback on
onStreamEnabled()
of theParticipant
withStream
object.
Events associated with muteMic
- Every Participant will receive a callback on
onStreamDisabled()
of theParticipant
withStream
object.
- Kotlin
- Java
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");
}
}
});
participant.addEventListener(new ParticipantEventListener() {
//Callback for when the participant starts a stream
@Override
public void 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
public void onStreamDisabled(Stream stream) {
if(stream.getKind().equals("audio")){
Log.d("VideoSDK","Audio Stream Off: onStreamDisabled" + stream);
}
}
});
Audio Permissions
- To use the microphone in a meeting, you need to add permission in
app/src/main/AndroidManifest.xml
after</application>
.
<uses-permission android:name="android.permission.RECORD_AUDIO" />
- You need to set up a permission request that provides this access.
- Kotlin
- Java
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()
}
}
}
}
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final int PERMISSION_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
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 boolean checkPermission() {
return ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
}
private void requestPermission() {
List<String> permissionList = new ArrayList<String>();
String[] permissions = {};
permissionList.add(Manifest.permission.RECORD_AUDIO);
ActivityCompat.requestPermissions(
this, permissionList.toArray(permissions),
PERMISSION_REQUEST_CODE
);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show();
// main logic
} else {
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
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