Change Audio Input Device - Flutter
During the room at any point a participant wishes to switch his/her input audio device, it can be done using the below mentioned methods.
On Android and iOS, the microphone and the speaker share a single audio route. Changing the microphone with changeMic() also changes where you hear audio, and setAudioDevice() changes both too.
getMics()
-
The
getMics()method of theVideoSDKclass lists the audio devices you can switch to, as a list ofMicrophoneDeviceInfoobjects. Each one is an audio route. -
The
MicrophoneDeviceInfoobject will contain thedeviceId,labelandkindfor the device. -
On Android, the devices are
SPEAKER_PHONE,EARPIECE,WIRED_HEADSETandBLUETOOTH, as each one is connected. On iOS, the list starts withSpeaker, which is not a microphone, followed by the microphones you can use, such asiPhone Microphone,Headphonesfor a wired headset, or the name of a Bluetooth headset.
changeMic()
-
After selecting the desired device, you can switch the audio input by passing its
deviceIdtochangeMic(deviceId: ...). -
During a room, calling
changeMic()without a device switches to the next device in the list. -
To switch to a custom microphone track instead, pass it as
changeMic(customMicrophoneTrack: track). This also unmutes the microphone. -
A muted microphone stays muted. If the microphone is on, it is turned off and on again on the new device, so you get
Events.streamDisabledand thenEvents.streamEnabledfor your audio stream. -
You can call
changeMic()with a device before you join. The room uses that device when you join. -
changeMic()returns aFuturethat throws aVideoSDKErrorif the switch fails, for example3059when the device is not available. The same error also reaches yourEvents.errorhandler. -
VideoSDK.getSelectedAudioDevice()returns the audio device in use, from join on, including the one you pick withchangeMic()orsetAudioDevice(). It isnullbefore join.
Example
import 'package:flutter/material.dart';
import 'package:videosdk/videosdk.dart';
class RoomScreen extends StatefulWidget {
const RoomScreen({Key? key}) : super(key: key);
@override
_RoomScreenState createState() => _RoomScreenState();
}
class _RoomScreenState extends State<RoomScreen> {
late Room _room;
List<MicrophoneDeviceInfo> mics = [];
@override
void initState() {
super.initState();
fetchMics();
}
void fetchMics() async {
List<MicrophoneDeviceInfo>? audioDevices = await VideoSDK.getMics();
setState(() {
// On iOS, "Speaker" is not a microphone.
mics = (audioDevices ?? [])
.where((device) => device.label != 'Speaker')
.toList();
});
}
Future<void> changeMic(MicrophoneDeviceInfo device) async {
try {
await _room.changeMic(deviceId: device.deviceId);
} on VideoSDKError catch (error) {
print("changeMic failed: ${error.code} ${error.name}");
}
}
@override
Widget build(BuildContext context) {
return Column(children: [
ElevatedButton(
child: Text("Change Input Device"),
onPressed: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text("Select Audio Device"),
content: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SingleChildScrollView(
reverse: true,
child: mics.isNotEmpty
? Column(
children: mics
.map(
(device) => ElevatedButton(
child: Text(device.label),
onPressed: () async {
Navigator.pop(context);
await changeMic(device);
},
),
)
.toList(),
)
: const Text("No Microphone Devices found."),
)
],
),
),
);
}),
]);
}
}
API Reference
The API references for all the methods and events utilized in this guide are provided below.
Got a Question? Ask us on discord

