Mute / Unmute Mic - Flutter
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 room participants, while unmuting it allows others to hear you.
unmuteMic()
-
By using
unmuteMic()function ofRoomobject, 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
streamsproperty ofParticipantobject.
On Android, the SDK does not ask for microphone permission. Call VideoSDK.requestPermissions() before unmuteMic(). Without the permission, the mic stays off: unmuteMic() throws error 3018, and the same error reaches Events.error.
muteMic()
-
By using
muteMic()function ofRoomobject, local participant can stop publish audio to other participants. -
You can call this method when the local participant is broadcasting any audio to others.
unmuteMic() and muteMic() return a Future that throws a VideoSDKError when the SDK refuses the call, for example 3054 when the mic is already on or off, or 3055 while another mic change is still running. await them inside try/catch. On Android, a call made while the room reconnects is reported on Events.error instead of being thrown, so keep an Events.error handler too.
Example
import 'package:flutter/material.dart';
import 'package:videosdk/videosdk.dart';
class RoomScreen extends StatefulWidget {
...
}
class _RoomScreenState extends State<RoomScreen> {
late Room _room;
@override
void initState() {
super.initState();
...
}
@override
Widget build(BuildContext context) {
return Column(
children:[
ElevatedButton(
onPressed: () async {
try {
await _room.unmuteMic();
} on VideoSDKError catch (error) {
print("unmuteMic failed: ${error.code} ${error.name}");
}
},
child: const Text("Unmute Mic"),
),
ElevatedButton(
onPressed: () async {
try {
await _room.muteMic();
} on VideoSDKError catch (error) {
print("muteMic failed: ${error.code} ${error.name}");
}
},
child: const Text("Mute Mic"),
),
]
);
}
}
Getting Participant Mic Status
- You can get the local participant's media status by going through the
streamsMap of theParticipantobject. - If
streamscontains a value wherekindisMediaType.AUDIOthen the participant's microphone is active else it is disabled. - Stream events are delivered on the
Room, so pass theRoomto the widget.participant.on()delivers the same events for one participant.stream.participantIdis the id of the participant the stream belongs to.
import 'package:flutter/material.dart';
import 'package:videosdk/videosdk.dart';
class ParticipantTile extends StatefulWidget {
final Room room;
final Participant participant;
const ParticipantTile(
{super.key, required this.room, required this.participant});
@override
State<ParticipantTile> createState() => _ParticipantTileState();
}
class _ParticipantTileState extends State<ParticipantTile> {
RTCStream? audioStream;
@override
void initState() {
super.initState();
// initial audio stream for the participant
//Check if audio stream is already present
audioStream = widget.participant.streams.values
.where((stream) => stream.kind == MediaType.AUDIO)
.firstOrNull;
_initStreamListeners();
}
//Change state according to the events received
void _initStreamListeners() {
widget.room.on(Events.streamEnabled, _onStreamEnabled);
widget.room.on(Events.streamDisabled, _onStreamDisabled);
}
bool _isAudioOfThisParticipant(RTCStream stream) =>
stream.participantId == widget.participant.id && stream.kind == MediaType.AUDIO;
void _onStreamEnabled(RTCStream stream) {
if (_isAudioOfThisParticipant(stream)) {
setState(() => audioStream = stream);
}
}
void _onStreamDisabled(RTCStream stream) {
if (_isAudioOfThisParticipant(stream)) {
setState(() => audioStream = null);
}
}
@override
void dispose() {
// The room outlives the tile, so remove the handlers from it.
widget.room.off(Events.streamEnabled, _onStreamEnabled);
widget.room.off(Events.streamDisabled, _onStreamDisabled);
super.dispose();
}
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Text("Current Mic status of ${widget.participant.displayName} is ${audioStream !=null ? "Active": "Disable"}")
);
}
}
Events associated with unmuteMic
- Every Participant will receive a callback on
Events.streamEnabledof theRoomobject withRTCStreamobject.stream.participantIdis the id of the participant who unmuted.
Events associated with muteMic
- Every Participant will receive a callback on
Events.streamDisabledof theRoomobject withRTCStreamobject.stream.participantIdis the id of the participant who muted.
Listen for these events with room.on(). You can also listen with participant.on(), which delivers only that participant's streams.
import 'package:flutter/material.dart';
import 'package:videosdk/videosdk.dart';
class ParticipantTile extends StatefulWidget {
final Room room;
final Participant participant;
...
}
class _ParticipantTileState extends State<ParticipantTile> {
@override
void initState() {
super.initState();
...
_initStreamListeners();
}
//Change state according to the events received
void _initStreamListeners() {
widget.room.on(Events.streamEnabled, _onStreamEnabled);
widget.room.on(Events.streamDisabled, _onStreamDisabled);
}
void _onStreamEnabled(RTCStream stream) {
if (stream.participantId == widget.participant.id && stream.kind == MediaType.AUDIO) {
//Mic Turned On
}
}
void _onStreamDisabled(RTCStream stream) {
if (stream.participantId == widget.participant.id && stream.kind == MediaType.AUDIO) {
//Mic Turned Off
}
}
@override
void dispose() {
widget.room.off(Events.streamEnabled, _onStreamEnabled);
widget.room.off(Events.streamDisabled, _onStreamDisabled);
super.dispose();
}
@override
Widget build(BuildContext context) {
return YourParticipantWidget();
}
}
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

